Click

A powerful and widely-used Python CLI library that uses decorators to define commands. Created by the Flask developer.

pythonclidecoratorflask

GitHub Overview

pallets/click

Python composable command line interface toolkit

Stars16,668
Watchers182
Forks1,465
Created:April 24, 2014
Language:Python
License:BSD 3-Clause "New" or "Revised" License

Topics

cliclickpalletspython

Star History

pallets/click Star History
Data as of: 7/25/2025, 02:00 AM

Framework

Click

Overview

Click is a powerful and beautiful library for building command-line interfaces in Python. It features a decorator-based approach for defining commands and was created by the developers of the Flask framework. It enables creating CLI applications with readable and maintainable code.

Details

Click was first released in 2014 and has been used as a standard in Python CLI development for many years. The current version is 8.x series, supporting Python 3.7 and later. With its decorator-based API, it enables intuitive and clean code writing, and is adopted by many prominent projects such as Flask, Poetry, Black, and pytest.

Key Features

  • Decorator-based API: Define commands concisely with decorators like @click.command()
  • Automatic help generation: Automatically generate help messages from command docstrings
  • Type validation: Type validation and error handling for arguments and options
  • Testing support: Automatic testing features for CLI applications with CliRunner
  • Subcommands: Support for creating applications with complex CLI structures
  • Color support: Color display functionality in terminal output

Pros and Cons

Pros

  • Maintainability: Maintainable code with declarative description using decorators
  • Rich functionality: Comprehensive built-in features including parameter validation, prompts, and file processing
  • Test-friendly: Comprehensive testing support with CliRunner
  • Ecosystem: Consistent development environment by the same Pallets Project as Flask
  • Track record: Adoption record in many enterprises and open-source projects

Cons

  • External dependency: Additional installation required as it's not in the standard library
  • Learning cost: Understanding of decorators and Click concepts required
  • Overhead: Features may be excessive for simple scripts

Key Links

Example Usage

import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for _ in range(count):
        click.echo(f'Hello, {name}!')

@click.group()
def cli():
    """A simple CLI with subcommands."""
    pass

@cli.command()
@click.argument('filename')
def process(filename):
    """Process the given file."""
    click.echo(f'Processing {filename}')

@cli.command()
@click.option('--verbose', is_flag=True, help='Enable verbose output.')
def status(verbose):
    """Show status information."""
    if verbose:
        click.echo('Verbose mode enabled')
    click.echo('Status: OK')

if __name__ == '__main__':
    hello()  # For single command
    # cli()  # For group commands