API Reference
Click
Python package for building beautiful command-line interfaces with composable commands and options.
Official Documentation ↗CompatibilityThis documentation covers version
v8.1. Check the official docs for the latest API surface.Installation
pip
pip install clickMethods
No methods match this filter.
Click is a Python package for creating command line interfaces in a composable way with as little code as necessary. It uses decorators to declare parameters and provides help generation, type validation, and nested commands.
Key Features
- Decorators: @command, @group, @option, @argument
- Types: Path, Choice, IntRange, DateTime, File
- Context: Shared state across commands
- Help: Auto-generated usage and help text
- Testing: CliRunner for in-process tests
Errors
The official API reference declares the following error types:
click.UsageError(exception) — official text: “exception click.UsageError(message, ctx=None) — An internal exception that signals a usage error. This typically aborts any further handling.” (source: https://click.palletsprojects.com/en/stable/api/#click.UsageError, fetched 2026-11-08T16:03:08.4363995+08:00)click.BadParameter(exception) — official text: “exception click.BadParameter(message, ctx=None, param=None, param_hint=None) — An exception that formats out a standardized error message for a bad parameter.” (source: https://click.palletsprojects.com/en/stable/api/#click.BadParameter, fetched 2026-11-08T16:03:08.4363995+08:00)click.FileError(exception) — official text: “exception click.FileError(filename, hint=None) — Raised if a file cannot be opened.” (source: https://click.palletsprojects.com/en/stable/api/#click.FileError, fetched 2026-11-08T16:03:08.4363995+08:00)click.ClickException(exception) — official text: “exception click.ClickException(message) — An exception that Click can handle and show to the user.” (source: https://click.palletsprojects.com/en/stable/api/#click.ClickException, fetched 2026-11-08T16:03:08.4363995+08:00)click.Abort(exception) — official text: “exception click.Abort — An internal signalling exception that signals Click to abort.” (source: https://click.palletsprojects.com/en/stable/api/#click.Abort, fetched 2026-11-08T16:03:08.4363995+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:03:08.4363995+08:00 via WebFetch 官方 API 页面(Exceptions 章节另抓 https://click.palletsprojects.com/en/stable/exceptions/).
@click.command→ signature from https://click.palletsprojects.com/en/stable/api/#click.command; official text: “click.command(name: Callable[[…], Any]) → Command”@click.group→ signature from https://click.palletsprojects.com/en/stable/api/#click.group; official text: “click.group(name: Callable[[…], Any]) → Group”@click.option→ signature from https://click.palletsprojects.com/en/stable/api/#click.option; official text: “click.option(*param_decls, cls=None, **attrs)”@click.argument→ signature from https://click.palletsprojects.com/en/stable/api/#click.argument; official text: “click.argument(*param_decls, cls=None, **attrs)”context→ signature from https://click.palletsprojects.com/en/stable/api/#click.pass_context; official text: “click.pass_context(f) — Parameters: f (t.Callable[te.Concatenate[Context, P], R]) Return type: t.Callable[P, R]”
FAQ
- How does Click compare to argparse?
- Click is decorator-based with less boilerplate, automatic help generation, and rich types like Path and Choice. argparse is stdlib with no dependencies. Click is more ergonomic for complex CLIs; argparse suits simple stdlib-only tools.
- How do I share state between commands?
- Use a context object via @click.pass_context and ctx.obj. Set ctx.ensure_object(dict) in the group, then read it in subcommands. This avoids globals and supports nested command groups.
- Can Click commands be tested?
- Yes. Use the CliRunner from click.testing: invoke result = runner.invoke(cli, ['--name', 'Al']) and assert on result.exit_code and result.output. This runs commands in-process without subprocess overhead.
Report a problem with this API docOpens a pre-filled issue on GitHub.