- Home
- Skills
- Vanman2024
- Cli Builder
- Typer Patterns
typer-patterns_skill
- Python
0
GitHub Stars
2
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill vanman2024/cli-builder --skill typer-patterns- QUICKSTART.md3.8 KB
- SKILL.md5.2 KB
Overview
This skill provides modern, type-safe Typer CLI patterns that leverage Python type hints, Enums, sub-application composition, and Typer() instance factories. It helps you build maintainable, testable command-line tools with consistent validation and clear help text. Use it to standardize CLI structure across projects and frameworks.
How this skill works
The skill supplies a set of reusable patterns and templates: typed command signatures, Enum-based options for constrained choices, sub-app composition for grouped commands, and a factory pattern for creating Typer instances. It includes validation scripts to enforce type hints and return annotations, plus example CLIs that show common integrations like custom validators and rich output.
When to use it
- Building a new CLI with Typer and wanting strong type safety and IDE support
- Converting ad-hoc scripts into organized, testable command-line tools
- Implementing constrained options where Enum selections and autocomplete are helpful
- Structuring a multi-command application with logical grouping and sub-apps
- Creating unit-testable CLI entry points using a Typer factory pattern
Best practices
- Always add Python type hints for every parameter and return value
- Prefer Enums over plain strings for constrained choice options
- Use pathlib.Path for file paths to leverage native validation
- Keep commands single-responsibility and small for easier testing
- Organize related commands into sub-apps and expose a factory create_app()
- Add docstrings to commands so Typer generates useful help text
Example use cases
- A data processing CLI with Path-typed input/output and an integer batch size
- An export command that accepts an OutputFormat Enum (json, yaml, text)
- A database sub-app with migrate and seed commands under a db namespace
- A web server command with a validated port callback to enforce allowed ports
- A testable CLI built via create_app() so tests can invoke commands programmatically
FAQ
No. Use Enums only for options with a fixed set of valid values where autocomplete and explicit choices improve UX.
How does the factory pattern help testing?
create_app() returns a Typer instance that tests can import and invoke without running the CLI process, enabling unit tests and mocking.