Skip to content
/ python-cli-template Public template

A template for building Python CLIs using Click

License

Notifications You must be signed in to change notification settings

ypk46/python-cli-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python CLI

A template for building Python CLIs.

Prerequisites

  • Python v3.9+

Getting Started

Development

  1. Install the dependencies in editable mode:

    pip install -e .
  2. Run the CLI:

    pycli --help

Using the template

  1. 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.
  2. 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 your ENTRYPOINT matches your new executable.

  3. Add the CLI description by changing the cli() method docstring at cli/__main__.py. This is the text that will be shown when calling pycli --help.

  4. Create click commands within the cli/commands directory. For demo purposes, we have included the cli/commands/demo.py so you can see how click commands are defined.

  5. Add your commands to the CLI by add the click command method in the cli/__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) <---
  6. Now you can run your command:

    pycli sum --num1 14 --num2 16

About

A template for building Python CLIs using Click

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published