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

pippip install click

Methods

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:

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/).

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.