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.tomlbased 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
Dockerfileso yourENTRYPOINTmatches 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
clickcommands within thecli/commandsdirectory. For demo purposes, we have included thecli/commands/demo.pyso you can see howclickcommands are defined. -
Add your commands to the CLI by add the
clickcommand method in thecli/__main__.pyas 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