Skip to content

Commit 958e878

Browse files
feat: add cli and tests
1 parent 3214f13 commit 958e878

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
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

1924
Print 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

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ license-files = ["LICEN[CS]E*"]
1515
dynamic = ["version"]
1616
dependencies = []
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]
1925
build = [
2026
"build==1.2.2.post1",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
"""Python Package Template"""
1+
"""Python CLI Template"""
22

33
__version__ = "0.0.0"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .cli import main
2+
3+
if __name__ == "__main__":
4+
main()

src/python_cli_template/cli.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

0 commit comments

Comments
 (0)