Generate shell completion scripts for Click apps.
Project description
click-complete - Shell Completion for Click CLI Applications
click-complete is a Python library that simplifies the process of adding shell completion support to Click-based CLI applications.
Features
- Shell Completion Generation: Automatically generate shell completion scripts for your Click CLI applications.
- Customizable Options: Add a
--completionoption to your CLI commands to generate completion scripts on the fly. - Support for Multiple Shells: Currently supports
fishshell, with extensibility forbashandzsh. - File Validation: Includes a custom
FileTypeparameter type for validating file paths and extensions.
Installation
You can install click-complete via pip:
pip install click-complete
Quick Start
Adding Shell Completion to a Click Command
Use the completion_option decorator to add a --completion option to your Click command:
import click
from click_complete import completion_option
@click.command()
@completion_option()
def mycli():
"""A sample CLI tool."""
click.echo("Welcome to my CLI!")
if __name__ == "__main__":
mycli()
Run the command with the --completion option to generate a completion script:
$ mycli --completion fish
# Fish completion script for mycli
Adding a Completion Command to a Click Group
Use the completion_command function to add a dedicated completion command to your Click group:
import click
from click_complete import completion_command
@click.group()
def cli():
"""A sample CLI group."""
pass
# Add the completion command
cli.add_command(completion_command(program_name="my_app"))
if __name__ == "__main__":
cli()
Run the completion command to generate a completion script:
$ my_app completion fish
# Fish completion script for my_app
Validating File Paths
Use the FileType parameter type to validate file paths and extensions:
import click
from click_complete import FileType
@click.command()
@click.argument("file", type=FileType([".json", ".txt"]))
def validate_file(file):
"""Validate a file path and extension."""
click.echo(f"File '{file}' is valid!")
if __name__ == "__main__":
validate_file()
Run the command with a valid file path:
$ validate_file example.json
File 'example.json' is valid!
API Reference
get_completion
def get_completion(
cli: Union[click.Group, click.Command],
program_name: str,
shell: str
) -> str:
"""
Generate a shell completion script for the given CLI program.
Args:
cli: The Click command or group to generate completion for.
program_name: The name of the CLI program.
shell: The type of shell to generate completion for (e.g., 'fish').
Returns:
The completion script as a string.
Raises:
click.UsageError: If the specified shell is not supported or not found.
"""
completion_option
def completion_option(
*param_decls: str,
program_name: Optional[str] = None,
**kwargs: Any,
) -> _Decorator[FC]:
"""
A decorator to add an option for generating shell completion scripts.
Parameters:
param_decls: Names of the option (e.g., "--completion").
program_name: The name of the program. If not provided, it will be inferred.
kwargs: Additional keyword arguments passed to `click.option`.
Returns:
A Click decorator for adding the completion option.
"""
completion_command
def completion_command(
program_name: Optional[str] = None,
**kwargs: Any,
) -> click.Command:
"""
Create a Click command to generate shell autocompletion scripts.
Args:
program_name: The name of the program. If not provided, it will be inferred.
**kwargs: Additional keyword arguments to customize the `@click.command`.
Returns:
A Click command to handle autocompletion generation.
"""
FileType
class FileType(click.ParamType):
"""
Custom Click parameter type to validate file paths and extensions.
Args:
extensions: A list of valid file extensions (e.g., ['.json', '.txt']).
case_sensitive: If False, extension checks will be case-insensitive.
readable: If True, the file must be readable.
exists: If True, the file must exist.
resolve_path: If True, resolve the path to an absolute path.
writable: If True, the file must be writable.
executable: If True, the file must be executable.
"""
Supported Shells
Currently, click-complete supports the following shells:
fishbash(coming soon)zsh(coming soon)
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
click-complete is licensed under the MIT License. See MIT License for more details.
Acknowledgments
- Built on top of the amazing Click library.
- Inspired by shellingham for shell detection.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file click_complete-0.1.0.tar.gz.
File metadata
- Download URL: click_complete-0.1.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
734378d788f5530cfb1f00883a211b674ccc9c67cceec2f527d620c22667e1cf
|
|
| MD5 |
3d5326de07e0bb3cc10c26f6fed3b067
|
|
| BLAKE2b-256 |
9cbef465ee0dddcf9d55f872e4b465823c533ae5a911d127aac97555678bb066
|
Provenance
The following attestation bundles were made for click_complete-0.1.0.tar.gz:
Publisher:
python-publish.yml on xMohnad/click-complete
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click_complete-0.1.0.tar.gz -
Subject digest:
734378d788f5530cfb1f00883a211b674ccc9c67cceec2f527d620c22667e1cf - Sigstore transparency entry: 170522305
- Sigstore integration time:
-
Permalink:
xMohnad/click-complete@d8c8ec2b6896878001f390ae85bb77b5a56c6ada -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/xMohnad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d8c8ec2b6896878001f390ae85bb77b5a56c6ada -
Trigger Event:
push
-
Statement type:
File details
Details for the file click_complete-0.1.0-py3-none-any.whl.
File metadata
- Download URL: click_complete-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f9b0e13466740fde55e3489291926f8bf7046bd9d6e9ae5d911e4c65b0b5d76
|
|
| MD5 |
195d6e0f9560c3faa985a6d6b1087a97
|
|
| BLAKE2b-256 |
07b39468ebc484709b0dcfcbb7b15c471826354f1a202d9c18d81666996bcc3f
|
Provenance
The following attestation bundles were made for click_complete-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on xMohnad/click-complete
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click_complete-0.1.0-py3-none-any.whl -
Subject digest:
9f9b0e13466740fde55e3489291926f8bf7046bd9d6e9ae5d911e4c65b0b5d76 - Sigstore transparency entry: 170522307
- Sigstore integration time:
-
Permalink:
xMohnad/click-complete@d8c8ec2b6896878001f390ae85bb77b5a56c6ada -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/xMohnad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d8c8ec2b6896878001f390ae85bb77b5a56c6ada -
Trigger Event:
push
-
Statement type: