🖱️

Click — The Cleanest Way to Build Python CLI Tools

How a decorator-based CLI framework works internally

Internals

@click.command() wraps the function in a Command object. @click.option() adds Option objects to the function's __click_params__ list.

On hello() call: Command.main() → parse sys.argv → map to function args → type conversion + validation → call original function.

Groups for Subcommands

Click internally routes subcommands via MultiCommand.invoke().

Relationship with Typer

Typer (by FastAPI's creator) layers type hints on top of Click. Same philosophy as FastAPI — define args via function signature type hints instead of decorators.

Key Points

1

@click.command() wraps function in Command object

2

@click.option() adds Option objects to __click_params__

3

On execution: parse sys.argv → type conversion → call function

4

@click.group() for subcommand structure (like git init, deploy)

Use Cases

CLI tools — deploy scripts, data pipelines, admin commands Django management command alternative — Click is cleaner sometimes