Skip to content

Commit 95c7666

Browse files
committed
feat(cli): add a small hello command
Show a minimal subcommand pattern without turning the template into a demo app.
1 parent 4694017 commit 95c7666

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ cheap enough to run all the time.
2020
make setup-dev
2121
make check
2222
python -m python_template --help
23+
python -m python_template hello
24+
```
25+
26+
Sample output:
27+
28+
```text
29+
Let's build something fun, Yujeong.
2330
```
2431

2532
## Core commands

src/python_template/__main__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,30 @@
1111
)
1212

1313

14-
@click.command(help="Show the template version and default tasks.")
15-
def cli() -> None:
16-
"""Print the template version and recommended first commands."""
14+
@click.group(
15+
invoke_without_command=True,
16+
context_settings={"help_option_names": ["-h", "--help"]},
17+
help="Show the template defaults and example commands.",
18+
)
19+
@click.pass_context
20+
def cli(ctx: click.Context) -> None:
21+
"""Run the template CLI."""
22+
if ctx.invoked_subcommand is not None:
23+
return
24+
1725
click.echo(f"python-template {__version__}")
1826
click.echo("Default tasks:")
1927
for task in DEFAULT_TASKS:
2028
click.echo(f"- {task}")
2129

2230

31+
@cli.command()
32+
@click.option("--name", default="Yujeong", show_default=True, help="Name to greet.")
33+
def hello(name: str) -> None:
34+
"""Print a tiny example subcommand."""
35+
click.echo(f"Let's build something fun, {name}.")
36+
37+
2338
def main() -> None:
2439
"""Run the CLI."""
2540
cli()

tests/test_cli.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,34 @@ def test_module_cli_help_runs() -> None:
3030
text=True,
3131
)
3232

33-
assert "Show the template version and default tasks." in result.stdout
33+
assert "Show the template defaults and example commands." in result.stdout
3434
assert "Usage:" in result.stdout
3535

3636

37+
def test_module_cli_hello_runs_with_default_name() -> None:
38+
"""Run the example hello command with the default name."""
39+
result = subprocess.run(
40+
[sys.executable, "-m", "python_template", "hello"],
41+
check=True,
42+
capture_output=True,
43+
text=True,
44+
)
45+
46+
assert "Let's build something fun, Yujeong." in result.stdout
47+
48+
49+
def test_module_cli_hello_accepts_a_custom_name() -> None:
50+
"""Run the example hello command with an explicit name."""
51+
result = subprocess.run(
52+
[sys.executable, "-m", "python_template", "hello", "--name", "friend"],
53+
check=True,
54+
capture_output=True,
55+
text=True,
56+
)
57+
58+
assert "Let's build something fun, friend." in result.stdout
59+
60+
3761
def test_package_exports_version() -> None:
3862
"""Expose the package version at the top level."""
3963
assert __version__ == "0.1.0"

0 commit comments

Comments
 (0)