A template for building Python CLIs.
- Python v3.9+
-
Install the dependencies in editable mode:
pip install -e .
-
Run the CLI:
pycli --help
-
Fill the
pyproject.toml
based on your CLI specification:name
: The name of the project.authors
: Your name or your organization name.description
: A brief description of the CLI project.
-
Define a custom CLI executable (optional):
[build-system] ... [project] ... [project.scripts] pycli = "cli.__main__:cli"
Allows you to run your CLI through:
pycli <command>
.After changing the CLI executable, make sure to update the
Dockerfile
so yourENTRYPOINT
matches your new executable. -
Add the CLI description by changing the
cli()
method docstring atcli/__main__.py
. This is the text that will be shown when callingpycli --help
. -
Create
click
commands within thecli/commands
directory. For demo purposes, we have included thecli/commands/demo.py
so you can see howclick
commands are defined. -
Add your commands to the CLI by add the
click
command method in thecli/__main__.py
as follow:@click.group() @click.version_option(__version__.version) def cli(): """ CLI template for Python projects. """ # Add commands to the CLI cli.add_command(cmd_sum) <---
-
Now you can run your command:
pycli sum --num1 14 --num2 16