Click

8.3.1 · active · verified Fri Mar 27

Click ('Command Line Interface Creation Kit') is a Python package for creating composable, beautiful command-line interfaces with minimal boilerplate. It uses a decorator-based API to turn functions into CLI commands with automatic help page generation, argument/option parsing, type coercion, and shell completion. Current version is 8.3.1 (latest stable); the project follows a feature-release / fix-release cadence under the Pallets organization, with feature releases (e.g. 8.2.0, 8.3.0) potentially introducing deprecations or breaking changes and patch releases (e.g. 8.3.1) being safe upgrades.

Warnings

Install

Imports

Quickstart

A minimal Click CLI with a group, a subcommand, options, and a test via CliRunner.

import click
from click.testing import CliRunner

@click.group()
@click.option('--verbose', is_flag=True, default=False, help='Enable verbose output.')
@click.pass_context
def cli(ctx, verbose):
    """My CLI tool."""
    ctx.ensure_object(dict)
    ctx.obj['verbose'] = verbose

@cli.command()
@click.option('--count', default=1, show_default=True, help='Number of greetings.')
@click.option('--name', prompt='Your name', help='Person to greet.')
@click.pass_context
def greet(ctx, count, name):
    """Greet NAME a number of times."""
    for _ in range(count):
        click.echo(f"Hello, {name}!")
    if ctx.obj.get('verbose'):
        click.echo(f"(verbose mode, greeted {count} time(s))")

if __name__ == '__main__':
    # Direct invocation
    cli()

# --- Testing ---
def test_greet():
    runner = CliRunner()
    result = runner.invoke(cli, ['greet', '--name', 'World', '--count', '2'])
    assert result.exit_code == 0, result.output
    assert result.output.count('Hello, World!') == 2

test_greet()
click.echo('Quickstart test passed.')

view raw JSON →