Skip to content

Commit a25be5e

Browse files
committed
feat: add cli option
1 parent c3a4900 commit a25be5e

6 files changed

Lines changed: 88 additions & 7 deletions

File tree

cookiecutter.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"version": "0.1.0",
55
"description": "What's the main goal of this package?",
66
"python_version": "3.13",
7-
"year": "2025"
7+
"year": "2025",
8+
"include_cli": "n"
89
}

{{cookiecutter.package_name}}/pyproject.toml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ authors = [
88
readme = "README.md"
99
license = { file = "LICENSE" }
1010
requires-python = ">={{cookiecutter.python_version}}"
11-
dependencies = []
11+
dependencies = [
12+
{% if cookiecutter.include_cli == 'y' %}
13+
"click==8.1.7",
14+
{% endif %}
15+
]
1216

1317
[project.optional-dependencies]
1418
dev = [
@@ -17,9 +21,11 @@ dev = [
1721
"pytest-cov==6.0.0",
1822
]
1923

20-
[build-system]
21-
requires = ["uv_build>=0.8.14,<0.9.0"]
22-
build-backend = "uv_build"
24+
{% if cookiecutter.include_cli == 'y' %}
25+
[project.scripts]
26+
{{cookiecutter.package_name}} = "{{cookiecutter.package_name}}.__main__:cli"
27+
{% endif %}
2328

24-
[tool.pytest.ini_options]
25-
pythonpath = ["src"]
29+
[build-system]
30+
requires = ["hatchling"]
31+
build-backend = "hatchling.build"

{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/add_one.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
from pathlib import Path
22

3+
{% if cookiecutter.include_cli == 'y' %}
4+
import click
5+
6+
7+
@click.command(
8+
help="""
9+
Add one to each line in the input file.
10+
"""
11+
)
12+
@click.option(
13+
"--input-path",
14+
type=click.Path(exists=True, path_type=Path),
15+
required=True,
16+
help="Path to the input files containing one integer per line.",
17+
)
18+
@click.option(
19+
"--output-path",
20+
type=click.Path(path_type=Path),
21+
required=True,
22+
help="Path to the output file containing the integers plus one.",
23+
)
24+
def add_one_cli(input_path: Path, output_path: Path) -> None:
25+
"""
26+
Add one to each line in the input file.
27+
"""
28+
with open(output_path, "w") as output_file:
29+
for n_plus_1 in add_one_file(input_path):
30+
output_file.write(str(n_plus_1) + "\n")
31+
{% endif %}
332

433
def add_one_file(input_file: Path) -> list[int]:
534
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import click
2+
3+
from {{cookiecutter.package_name}}.add_one import add_one_cli
4+
5+
@click.group()
6+
def cli() -> None:
7+
pass
8+
9+
10+
# Add subcommands.
11+
cli.add_command(add_one_cli)

{{cookiecutter.package_name}}/tests/__init__.py

Whitespace-only changes.

{{cookiecutter.package_name}}/tests/test_add_one.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
from {{cookiecutter.package_name}}.add_one import add_one, add_one_file
2+
{% if cookiecutter.include_cli == 'y' %}
3+
from {{cookiecutter.package_name}}.add_one import add_one_cli
4+
from {{cookiecutter.package_name}}.__main__ import cli
5+
6+
from click.testing import CliRunner
7+
import tempfile
8+
{% endif %}
9+
210
from pathlib import Path
311

12+
{% if cookiecutter.include_cli == 'y' %}
13+
def test_add_one_cli(test_data_dir: Path) -> None:
14+
"""
15+
Test the add_one_cli function works correctly.
16+
"""
17+
input_path = test_data_dir / "test_add_one" / "integers.txt"
18+
19+
with tempfile.TemporaryDirectory() as tmp_dir:
20+
output_path = Path(tmp_dir) / "output.txt"
21+
22+
runner = CliRunner()
23+
result = runner.invoke(
24+
cli,
25+
[
26+
"add-one-cli",
27+
"--input-path",
28+
str(input_path),
29+
"--output-path",
30+
str(output_path),
31+
],
32+
)
33+
34+
assert result.exit_code == 0
35+
assert output_path.read_text() == "2\n3\n4\n"
36+
{% endif %}
37+
438
def test_add_one_file(test_data_dir: Path) -> None:
539
"""
640
Test that the add_one_file function works correctly.

0 commit comments

Comments
 (0)