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.
pip install clickVerified import paths — ran on the pinned version, not inferred.
A minimal Click CLI with a group, a subcommand, options, and a test via CliRunner.
Upgrade Python to >=3.10 (required by current 8.3.x) or pin 'click<8.2' if stuck on older Python.
Replace BaseCommand subclasses with Command and MultiCommand subclasses/isinstance checks with Group.
Use 'importlib.metadata.version("click")' for runtime version detection.Use 'shutil.get_terminal_size()' from the Python standard library instead.
Pass catch_exceptions=False to CliRunner.invoke() during development, or always assert result.exception is None and result.exit_code == 0 in tests.
Run CliRunner tests sequentially. Use runner.isolated_filesystem() for file-based tests to avoid cross-test contamination.
Invoke programmatically with standalone_mode=False: result = my_command.main(args=[], standalone_mode=False) to get the return value instead of a sys.exit().
Ensure the script is invoked with the intended command and arguments. If the application should perform an action without an explicit command, configure a default command or handle argument parsing explicitly.
Install the 'click' library using pip: `pip install click`. If using a virtual environment, ensure it is activated before installation.
Provide the expected subcommand as an argument (e.g., `your_script.py subcommand`), or add `invoke_without_command=True` to your `@click.group()` decorator if the group should run its callback even without a subcommand. Consult the help page with `your_script.py --help` to see available commands.
Ensure that your top-level command or group is executed via `if __name__ == '__main__': your_cli_function()`. If you need to invoke a subcommand from within another command's callback, use `ctx.invoke(subcommand_callback, ...)` or `group_object.invoke(ctx)`.
Supply the missing argument when running the command (e.g., `your_script.py <value_for_name>`). Review the command's expected arguments and options using `your_script.py --help`.