Typer: Build Powerful CLIs with Ease
Typer is a library for building Command Line Interface (CLI) applications that users will love to use and developers will love to create.
Overview
Typer is a modern, intuitive library for building robust and elegant command-line applications. It's designed to be easy to use, leveraging Python's type hints to create CLIs with minimal code. You write a standard Python function with type-annotated parameters, and Typer handles the parsing, validation, and help text generation for you.
Built on top of the powerful Click library, Typer provides a best-in-class developer experience without sacrificing power or extensibility. It comes with out-of-the-box support for shell autocompletion, rich and beautiful help messages, and a simple way to structure complex applications with subcommands. The result is a CLI that is not only functional but also a pleasure for your users to interact with.
Key Concepts
- Type-Hint Based CLIs Creating Your First Parameterized Command: The core of Typer. Define your CLI parameters simply by using standard Python type hints in your function signatures. Typer automatically converts them into CLI arguments and options.
- Commands and Subcommands Organizing Subcommands and Groups: Structure your application by decorating functions with
@app.command(). You can easily create nested, multi-level CLI tools by adding one Typer application to another. - CLI Arguments and Options Parameters and Inputs: Differentiate between positional arguments and named options (e.g.,
--verbose). Typer makes it simple to define defaults, enforce required values, and configure their behavior directly in the function definition. - Automatic Help and Rich Formatting Help and Rich Formatting: Typer automatically generates clear and helpful documentation for your CLI. With its deep integration with the Rich library, you get beautifully formatted, colorized help text and error messages with no extra configuration.
- Shell Completion Overview: Provide a fantastic user experience with automatic shell completion. Typer makes it trivial to add support for tab-completion in Bash, Zsh, Fish, and PowerShell, helping users discover and use your commands.
- Callbacks and Context Application Callbacks: Manage global state and execute pre-command logic using callbacks. This is perfect for setting up configurations, handling version flags, or validating context before a command runs.
Common Use Cases
- Building simple, single-file utility scripts.
- Creating complex, multi-command tools for development workflows (e.g., database managers, project scaffolders).
- Developing automation and DevOps scripts that require clear, validated inputs.
- Wrapping data processing or machine learning pipelines in a user-friendly command-line interface.
Getting Started
The best way to start is by following the Getting Started guide, which walks you through creating your first simple application. From there, you can learn how to add CLI Arguments and CLI Options to make your commands more powerful.