File tree Expand file tree Collapse file tree 6 files changed +78
-6
lines changed
tests/python_cli_template Expand file tree Collapse file tree 6 files changed +78
-6
lines changed Original file line number Diff line number Diff line change 66
77🐍 Python CLI Template
88
9+ ## Prerequisites
10+
11+ - [ Python] ( https://www.python.org/ )
12+ - [ pipx] ( https://pipx.pypa.io/ )
13+
914## Install
1015
1116[ Python] ( https://pypi.org/project/python_cli_template/ ) :
1217
1318``` sh
14- pip install python_cli_template
19+ pipx install python_cli_template
1520```
1621
1722## Usage
1823
1924Print greeting:
2025
21- ``` py
22- from python_cli_template import template
23-
24- print (template.hello())
26+ ``` sh
27+ python_cli_template --name world
2528```
2629
2730## License
Original file line number Diff line number Diff line change @@ -15,6 +15,12 @@ license-files = ["LICEN[CS]E*"]
1515dynamic = [" version" ]
1616dependencies = []
1717
18+ [project .scripts ]
19+ python_cli_template = " python_cli_template.cli:main"
20+
21+ [project .entry-points ."pipx .run" ]
22+ python_cli_template = " python_cli_template.cli:main"
23+
1824[project .optional-dependencies ]
1925build = [
2026 " build==1.2.2.post1" ,
Original file line number Diff line number Diff line change 1- """Python Package Template"""
1+ """Python CLI Template"""
22
33__version__ = "0.0.0"
Original file line number Diff line number Diff line change 1+ from .cli import main
2+
3+ if __name__ == "__main__" :
4+ main ()
Original file line number Diff line number Diff line change 1+ from argparse import ArgumentParser
2+
3+ from python_cli_template import __version__
4+ from python_cli_template .template import hello
5+
6+
7+ def main (argv : list [str ] = None ) -> None :
8+ parser = ArgumentParser (description = "Python CLI Template" )
9+
10+ parser .add_argument (
11+ "--version" ,
12+ "-v" ,
13+ action = "version" ,
14+ version = f"%(prog)s { __version__ } " ,
15+ )
16+
17+ parser .add_argument (
18+ "--name" ,
19+ "-n" ,
20+ default = "World" ,
21+ help = "name to greet [default: World]" ,
22+ )
23+
24+ parser .add_argument (
25+ "--color" ,
26+ "-c" ,
27+ action = "store_true" ,
28+ help = "colorize the text" ,
29+ )
30+
31+ args = parser .parse_args (argv )
32+
33+ color = "\033 [95m" if args .color else ""
34+ print (f"{ color } { hello (args .name )} " )
35+
36+
37+ if __name__ == "__main__" :
38+ main ()
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from python_cli_template .cli import main
4+
5+
6+ def test_hello (capsys ):
7+ assert main () is None
8+ captured = capsys .readouterr ()
9+ assert captured .out == "Hello, World!\n "
10+
11+
12+ def test_hello_world (capsys ):
13+ assert main (["--name" , "world" ]) is None
14+ captured = capsys .readouterr ()
15+ assert captured .out == "Hello, world!\n "
16+
17+
18+ def test_invalid (capsys ):
19+ with pytest .raises (SystemExit ) as exception :
20+ main (["--invalid" ])
21+ assert exception .type is SystemExit
You can’t perform that action at this time.
0 commit comments