Coverage for src / puzzletree / cli / __init__.py: 100.00%
2 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-12 20:35 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-12 20:35 +0000
1"""Puzzletree CLI application module.
3----------------
5This module initializes the CLI for the `puzzletree` package using the Typer library.
6It serves as the entry point for Puzzle reconstruction experiments and CLI tooling.
8Each command is defined in its respective module and registered via Typer to allow
9for a modular and extendable CLI application.
11Once installed, the `pyproject.toml` defines the entry point as
13``` py title="pyproject.toml"
14[project.scripts]
15puzzletree = "puzzletree:cli"
16```
18since in the puzzletree `src/puzzletree/__init__.py` we have the following import
20```python title="puzzletree/__init__.py"
21from puzzletree.cli import cli
22```
24Once installed this allows you to run the CLI directly from the command line using:
25```bash
26puzzletree [COMMAND] [OPTIONS]
27```
28or
30```bash
31uv run python -m puzzletree [COMMAND] [OPTIONS]
32```
34This module is designed to be run as a script, and it will automatically
35load the appropriate subcommands based on the environment and configuration.
36It also provides a help message when no command is specified.
39Example:
40 python -m puzzletree reconstruct --input-dir ./puzzletree-city-tiles
42Dependencies:
43 - Typer: A modern library for building CLI applications, based on Click.
44 - Pydantic: For data validation and settings management.
45 - Rich: For rich text and beautiful formatting in the terminal.
46 - Pydantic-settings: For settings management with Pydantic.
48Each subcommand is defined in its own module and registered automatically via dynamic discovery.
50**Adding New Commands:**
52To add a new CLI command, create a new Python module in the `puzzletree/cli/` directory and define a function with a `.command` attribute:
54```python
55from typer import Context
57def my_command(ctx: Context, ...):
58 \"\"\"My command description.\"\"\"
59 ...
61# Mark this function as a CLI command
62my_command.command = "my-command"
63```
65The command will be automatically discovered and registered when the CLI application starts. No manual registration is required.
67Subcommands can define a default behavior through the use of a @app.callback() function. This callback is invoked when the subcommand is used without specifying a sub-subcommand.
69For example, in the dataset command, the @app.callback() handles dataset loading when no sub-subcommand (like list) is provided. This enables users to run puzzletree dataset --name my_dataset directly, without needing to call a secondary command.
71If we want to add sub-subcommands (such as list), we define them using the @app.command() decorator. In this case, the callback is still invoked before the sub-subcommand is executed unless invoke_without_command=True is used, allowing for flexible pre-processing or shared setup logic.
73Commands:
74 - Commands are automatically discovered from modules in the cli directory
75"""
77from puzzletree.cli.main_cli import cli_app as cli
79__all__: list[str] = ["cli"]