Python library for building elegant CLI applications with minimal code. Use when creating Python scripts that need to parse command-line arguments, options, or flags. Specifically use for: (1) Creating CLI tools with argument parsing, (2) Building command-line interfaces with multiple subcommands, (3) Adding help text and documentation to scripts, (4) Implementing prompts, progress bars, and colored output, (5) Building CLI apps with type safety using Python type hints
Typer is a Python library for building CLI applications using type hints. It's built on Click but provides an intuitive, FastAPI-like developer experience.
Use this skill when:
git commit, git push)uv add typer
import typer
def main(name: str, count: int = 1):
"""Say hello multiple times."""
for _ in range(count):
typer.echo(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
Run:
$ python main.py Alice --count 3
Hello Alice
Hello Alice
Hello Alice
script.py name age)-- prefix (e.g., script.py --name Alice)# name is required argument, --count is optional option
def main(name: str, count: int = 1):
pass
For multiple commands, use typer.Typer():
import typer
app = typer.Typer()
@app.command()
def create(name: str):
"""Create a user."""
print(f"Creating user: {name}")
@app.command()
def delete(name: str):
"""Delete a user."""
print(f"Deleting user: {name}")
if __name__ == "__main__":
app()
Typer uses Python type hints for automatic validation and conversion:
def process(
name: str, # String
age: int, # Integer
height: float, # Float
active: bool = False, # Boolean flag (--active/--no-active)
):
pass
str, int, float - Basic typesbool - Creates --flag/--no-flag optionslist[str] - Multiple valuesdatetime - Date/time parsingenum.Enum - Choice validationpathlib.Path - Path validationtyping.Optional[T] - Optional valuesSee references/parameter_types.md for all supported types.
def main(name: str, age: int):
"""Required arguments, order matters."""
print(f"{name} is {age} years old")
Usage: python main.py Alice 30
def main(
name: str,
age: int = typer.Option(20, help="Age in years"),
verbose: bool = False,
):
"""Optional parameters with defaults."""
pass
Usage: python main.py Alice --age 30 --verbose
def main(
name: str = typer.Argument(..., help="User name"),
count: int = typer.Option(1, help="Number of times"),
):
pass
Create CLI apps with subcommands (like git):
import typer
app = typer.Typer()
@app.command()
def deploy(env: str = typer.Option("dev", help="Environment to deploy")):
"""Deploy application."""
print(f"Deploying to {env}")
@app.command()
def rollback(version: str = typer.Argument(..., help="Version to rollback")):
"""Rollback to a specific version."""
print(f"Rolling back to {version}")
if __name__ == "__main__":
app()
Usage:
$ python main.py deploy --env prod
$ python main.py rollback v1.2.3
Organize commands into groups:
app = typer.Typer()
users_app = typer.Typer()
app.add_typer(users_app, name="users", help="Manage users")
@users_app.command()
def create(name: str):
print(f"Creating user: {name}")
@users_app.command()
def delete(name: str):
print(f"Deleting user: {name}")
Usage:
$ python main.py users create Alice
$ python main.py users delete Alice
See references/commands.md for detailed command organization.
Use Rich for beautiful progress displays:
import typer
from rich.progress import track
app = typer.Typer()
@app.command()
def process():
"""Process items with progress bar."""
for item in track(range(100), description="Processing..."):
# Your processing logic
pass
if __name__ == "__main__":
app()
See references/progress.md for more progress bar options.
Prompt users for input:
def main(
name: str = typer.Option(..., prompt=True),
password: str = typer.Option(..., prompt=True, confirmation_prompt=True, hide_input=True),
):
"""Prompt for user input."""
print(f"Hello {name}")
Read values from environment variables:
def main(
api_key: str = typer.Option(None, envvar="API_KEY"),
):
"""Get API key from environment."""
print(f"API key: {api_key}")
Test Typer apps with CliRunner:
from typer.testing import CliRunner
from main import app
runner = CliRunner()
result = runner.invoke(app, ["Alice", "--count", "3"])
assert result.exit_code == 0
assert "Hello Alice" in result.stdout
parameter_types.md - All supported parameter types with examplescommands.md - Commands, subcommands, and CLI organizationprogress.md - Progress bars and spinnersNo executable scripts included. Typer is a framework, not a collection of tools.
No assets included. This is a pure Python library.
typer.echo() instead of print() for consistent output--help to see generated documentationtyper.Option() and typer.Argument() for advanced configuration