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 6
6
7
7
🐍 Python CLI Template
8
8
9
+ ## Prerequisites
10
+
11
+ - [ Python] ( https://www.python.org/ )
12
+ - [ pipx] ( https://pipx.pypa.io/ )
13
+
9
14
## Install
10
15
11
16
[ Python] ( https://pypi.org/project/python_cli_template/ ) :
12
17
13
18
``` sh
14
- pip install python_cli_template
19
+ pipx install python_cli_template
15
20
```
16
21
17
22
## Usage
18
23
19
24
Print greeting:
20
25
21
- ``` py
22
- from python_cli_template import template
23
-
24
- print (template.hello())
26
+ ``` sh
27
+ python_cli_template --name world
25
28
```
26
29
27
30
## License
Original file line number Diff line number Diff line change @@ -15,6 +15,12 @@ license-files = ["LICEN[CS]E*"]
15
15
dynamic = [" version" ]
16
16
dependencies = []
17
17
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
+
18
24
[project .optional-dependencies ]
19
25
build = [
20
26
" build==1.2.2.post1" ,
Original file line number Diff line number Diff line change 1
- """Python Package Template"""
1
+ """Python CLI Template"""
2
2
3
3
__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