Completion
Generate shell completion scripts with argcomplete.
User guide: Shell completion.
Usage
from clak import CompCmdRender, Command, Parser
class App(Parser):
completion = Command(CompCmdRender, help="Print shell completion script")
clak.comp.completion
Register a Python executable for use with the argcomplete module.
To perform the registration, source the output of this script in your bash shell (quote the output to avoid interpolation).
Example:
$ eval "$(register-python-argcomplete my-favorite-script.py)"
For Tcsh
$ eval `register-python-argcomplete --shell tcsh my-favorite-script.py`
For Fish
$ register-python-argcomplete --shell fish my-favourite-script.py > ~/.config/fish/my-favourite-script.py.fish
CompCmdRender
Bases: CompRenderCmdMixin, Parser
Command completion renderer class.
Combines the CompRenderCmdMixin with the base Parser to create a class that can render command completion code. This class provides the core functionality for generating shell completion scripts for command-line tools.
Key features: - Generates shell completion code for bash/tcsh/fish - Supports external completion scripts - Configurable executable names - Default completion behavior
Source code in clak/comp/completion.py
CompRenderCmdMixin
Bases: CompRenderMixin
Completion command support
Source code in clak/comp/completion.py
cli_run(ctx, **kwargs)
Command completion support mixin.
Adds command completion support to parsers by providing arguments to configure shell completion behavior:
- --no-defaults: Disable fallback to readline defaults (bash only)
- --complete-arguments: Custom completion arguments (bash only)
- --shell: Target shell (bash, zsh, tcsh, fish, powershell)
- --executable: Name of executable to complete
The mixin generates the appropriate shell completion code when run. Supports bash (default), zsh, tcsh, fish and powershell shells.
Example
my-app completion # Outputs bash completion code my-app completion --shell zsh # Outputs zsh completion code
Source code in clak/comp/completion.py
CompRenderMixin
Completion support Methods
Source code in clak/comp/completion.py
print_completion_stdout(args)
Print completion script to stdout.
Generates and outputs shell completion code for the specified executable using argcomplete. The completion script enables tab completion for commands and arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
SimpleNamespace
|
Namespace containing completion configuration: executable: Name of executable to enable completion for use_defaults: Whether to fallback to readline defaults (bash only) shell: Target shell (bash, zsh, tcsh, fish, powershell) complete_arguments: Optional arguments to pass to complete command external_argcomplete_script: Optional external completion script |
required |
Source code in clak/comp/completion.py
CompRenderOptMixin
Bases: CompRenderMixin
Completion options support mixin.
Adds a --completion flag that prints bash argcomplete shellcode
instead of running the command. Prefer :class:CompCmdRender as a
completion subcommand when you need --shell / --executable.
Example::
my-app --completion
Source code in clak/comp/completion.py
cli_run(ctx, **kwargs)
Print bash completion shellcode when --completion is set.
Example::
my-app --completion