Configs2026年4月14日·1 分钟阅读

Typer — Build Python CLIs from Type Hints

Typer turns Python functions into CLI commands using only type hints. It is built on Click but removes the boilerplate — write a function, and Typer auto-generates parsing, help text, and shell completions.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Stage only
信任
信任等级:Established
入口
step-1.md
安全暂存命令
npx -y tokrepo@latest install 6942f03e-37b5-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

TL;DR
Typer converts Python functions with type hints into full CLI commands with auto-generated parsing, help text, and completions.
§01

What it is

Typer is a Python library that turns regular Python functions into command-line interface commands using only type hints. Built on top of Click, Typer removes the boilerplate of argument parsing, help text generation, and shell completion setup.

Typer is created by the same developer behind FastAPI. It follows the same philosophy: use standard Python type annotations to define the interface, and let the framework handle the rest.

§02

How it saves time or tokens

Traditional CLI libraries like argparse require verbose argument definitions separate from the function logic. Typer eliminates this duplication. The function signature is the CLI definition. A single function with type-annotated parameters becomes a complete CLI command with help text, validation, and tab completion.

For AI agents generating CLI tools, Typer is the shortest path from a Python function to a working command. The type-hint-driven approach means LLMs can produce correct CLI code with minimal context.

§03

How to use

  1. Install Typer:
pip install typer
  1. Write a function with type hints:
import typer

def greet(name: str, count: int = 1, formal: bool = False):
    greeting = 'Good day' if formal else 'Hello'
    for _ in range(count):
        print(f'{greeting}, {name}')

if __name__ == '__main__':
    typer.run(greet)
  1. Run it from the terminal:
python greet.py --name World --count 3 --formal

Typer auto-generates --help, validates types, and provides shell completion.

§04

Example

import typer

app = typer.Typer()

@app.command()
def deploy(env: str = typer.Argument(..., help='Target environment'),
           dry_run: bool = typer.Option(False, help='Preview without applying')):
    if dry_run:
        print(f'Would deploy to {env}')
    else:
        print(f'Deploying to {env}...')

@app.command()
def rollback(env: str, steps: int = 1):
    print(f'Rolling back {steps} step(s) in {env}')

if __name__ == '__main__':
    app()

This creates a CLI with deploy and rollback subcommands, each with typed arguments and auto-generated help.

§05

Related on TokRepo

§06

Common pitfalls

  • Forgetting that Typer requires Python 3.7+ for proper type hint support. Older Python versions will not work correctly with the annotation-driven approach.
  • Not installing the shellingham and click-completion extras for shell completion. Run pip install 'typer[all]' to get the full feature set.
  • Using mutable default arguments (like lists or dicts) as parameter defaults. Use typer.Option(default_factory=list) instead to avoid shared state bugs.
  • Failing to review community discussions and changelogs before upgrading. Breaking changes in major versions can disrupt existing workflows. Pin versions in production and test upgrades in staging first.

常见问题

How does Typer compare to Click?+

Typer is built on top of Click and uses it under the hood. The difference is the developer experience: Click requires decorators and explicit parameter definitions, while Typer infers everything from function signatures and type hints. Typer produces the same result with less code.

Can Typer create multi-command CLI applications?+

Yes. Use the typer.Typer() app object and decorate functions with @app.command(). Each decorated function becomes a subcommand. You can also nest Typer apps for complex CLI hierarchies with command groups.

Does Typer support shell completion?+

Yes. Typer generates shell completion scripts for bash, zsh, fish, and PowerShell. Install with typer[all] and run the completion install command. Arguments, options, and even Enum values get tab-completed automatically.

Who created Typer?+

Typer was created by Sebastian Ramirez, the same developer behind FastAPI. Both libraries share the philosophy of using Python type annotations to define interfaces and auto-generate documentation and validation.

Can AI agents use Typer to build CLI tools?+

Yes. Typer's type-hint-driven approach is well-suited for AI code generation. An LLM can produce a complete CLI tool by writing a single Python function with type annotations. The minimal boilerplate means fewer opportunities for generation errors.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产