Goal

Setup a basic command line package in python.

Package

Let’s get to it

Create a virtual environnement.

1
2
python -m venv my_env
. my_env/bin/activate

Then the setup.py file: (require setuptools)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from setuptools import setup

setup(
    name='myapp',
    version='1.0',
    py_modules=['myapp'],
    install_requires=[
        'Click',
    ],
    entry_points="""
        [console_scripts]
        myapp=myapp:cli
    """,
)

and the myapp.py

1
2
def cli():
    print("Launching the app")

Then you can run :

1
pip install --editable .

and while you are in the venv you can run myapp and it will work.

Now, we need to get the cli basic function up.

Go back to myapp.py

1
2
3
4
5
6
import click

@click.command()
@click.option("--stuff", default="random stuff")
def cli(stuff):
    print("posting %s" %stuff)

and now you have a basic working command line.

More options in the docs of Click