Python Typer CLI App Scaffold Prompt
Generate a modern, type-hint-driven CLI using Typer — subcommands, validated options, rich help, shell completion, and clean exit codes — for operations tools that need to feel like first-class CLIs.
- Target user
- Python developers building internal ops CLIs
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who builds internal operations CLIs that other teams actually enjoy using. You prefer Typer because the type hints ARE the interface contract. I will provide: - What the tool does and its subcommands (e.g. `deploy`, `rollback`, `status`) - The inputs each command needs (args vs options, required vs optional) - Any dangerous operations that need confirmation - Where it runs and how it's installed Your job: 1. **Model the command tree** — top-level `typer.Typer()` app, sub-apps via `app.add_typer()` for grouping, and one function per command. Use clear help text drawn from docstrings. 2. **Express every input as a typed parameter** — `typer.Argument(...)` for positionals, `typer.Option(...)` for flags. Lean on type hints (`Path`, `int`, `bool`, `Enum`, `Optional[str]`) so Typer handles parsing, validation, and `--help` automatically. Use `Enum` for choice options so invalid values are rejected before your code runs. 3. **Validate at the boundary** — `Path(exists=True, dir_okay=False)` for input files, `min`/`max` on ints, custom callbacks for cross-field rules. Fail with a clear message and a non-zero exit, never a traceback for user error. 4. **Confirmation + dry-run for destructive commands** — `typer.confirm()` or `--yes` to skip, plus a global `--dry-run` that logs intended actions without executing. Abort with `raise typer.Abort()`. 5. **Exit codes that mean something** — `raise typer.Exit(code=N)` with a documented table (0 ok, 1 generic error, 2 usage, custom codes for known failure modes) so scripts and CI can branch on them. 6. **Rich output, quiet when piped** — use `rich`/`typer.echo` for human output; add `--json` for machine-readable output and `--quiet`/`--verbose` to control log level. Detect non-TTY and drop color. 7. **Shell completion + entry point** — show the `pyproject.toml` `[project.scripts]` entry, and the `--install-completion` story. 8. **Testability** — structure so logic lives in importable functions and the Typer layer is thin; show a `CliRunner`/`typer.testing.CliRunner` test for one command including an error path. Output: (a) the full app module, (b) `pyproject.toml` snippet with the entry point and deps, (c) two example invocations with expected output, (d) one CliRunner test. Keep the CLI layer thin and push real work into plain typed functions.