Click Completion
click-completion is a Click extension that provides shell completion for Fish, Bash, Zsh, and PowerShell. It helps users get tab-completion for their Click-based command-line interfaces. The current version is 0.5.2, and as a `click-contrib` library, it typically maintains an active but often lower-cadence release cycle, with updates often aligning with Click's own evolution.
Common errors
-
AttributeError: module 'click_completion.extra' has no attribute 'install'
cause Attempting to use `click_completion.extra.install()` or other functions from the `extra` module, which were removed in version 0.5.0.fixRemove all calls to `click_completion.extra` functions. `click-completion` 0.5.0 and later requires manual generation and sourcing of completion scripts. -
TypeError: startswith() missing 1 required positional argument: 'prefix'
cause The signature of `click_completion.core.startswith` changed in version 0.5.0, adding a required `prefix` argument. Code written for older versions might be calling it incorrectly.fixReview calls to `click_completion.core.startswith`. Ensure you are passing the `prefix` argument as required by the current version's API. -
Command 'my-cli-app' tab-completion is not working, or only completes basic commands, not options/arguments.
cause This is a common issue often caused by one of two things: `click_completion.init()` was not called in the Python application, or the generated shell completion script has not been correctly sourced in the user's shell.fix1. Verify `click_completion.init()` is called in your Click application. 2. Ensure the user has added the correct sourcing command to their shell configuration file (e.g., `echo 'source <(my-cli-app --show-completion bash)' >> ~/.bashrc && source ~/.bashrc`).
Warnings
- breaking Starting with version 0.5.0, `click-completion` no longer attempts to automatically install completion scripts. Users must manually generate and source the completion script for their shell.
- deprecated The `click_completion.extra` module and its functions, such as `install()` and `shell_paths()`, were removed in version 0.5.0.
- gotcha For completion to work correctly, `click_completion.init()` must be called within your Click application, and the generated completion script must be sourced in the user's shell environment.
Install
-
pip install click-completion
Imports
- init
from click_completion.core import init
from click_completion import init
- BashCompletion
from click_completion.core import BashCompletion
Quickstart
import click
import click_completion
# Initialize click-completion. This must be called BEFORE defining your commands
# if you want options like --show-completion to be available.
click_completion.init()
@click.group()
def cli():
"""A simple CLI demonstrating completion."""
pass
@cli.command()
@click.option('--name', default='World', help='The name to greet.')
def hello(name):
"""Greets the given name."""
click.echo(f"Hello, {name}!")
@cli.command()
def goodbye():
"""Says goodbye."""
click.echo("Goodbye!")
if __name__ == '__main__':
cli()