DEV Community

Cover image for Add helpful cli to your python libraries... All of them!
Waylon Walker
Waylon Walker

Posted on • Originally published at waylonwalker.com

Add helpful cli to your python libraries... All of them!

cli tools are super handy and easy to add to your python libraries to supercharge them. Even if your library is not a cli tool there are a number of things that a cli can do to your library.

Example Ideas

Things a cli can do to enhance your library.

🆚 print version
📃 print config
🕶 print readme
📝 print changelog
✏ change config
👩‍🎓 run a tutorial
🏗 scaffold a project with cookiecutter

🖱 Click

Click is the most popular python cli tool framework for python. There are others, some old, some new comers that make take the crown. For now Click is the gold standard if you want to make a powerful cli quickly. If you are dependency conscious and dont need a lot of tooling, use argparse.

Project Structure

.
├── setup.py
└── simple_click
    ├── cli.py
    └── __init__.py
Enter fullscreen mode Exit fullscreen mode

❯ cli.py

# simple_click/cli.py
import click

__version__ = "1.0.0"

@click.group()
def cli():
   pass

@cli.command()
def version():
    """prints project version"""
    click.echo(__version__)


if __name__ == '__main__':
    cli()
Enter fullscreen mode Exit fullscreen mode

✨ __init__.py

For our simple_click library __init__.py__ can be left empty. It is here purely to signify that simple_click is a library. It is likely that you will import other modules here that need to reside at the top level of your library api, your cli does not need to be at the top of of your api.

# __init__.py
Enter fullscreen mode Exit fullscreen mode

🚪 Entry Points

Entry points are the magic that make python cli tools available as their own command without having python before it or the file extension.

# setup.py

from setuptools import setup, find_packages

# this is the 🥩 meat of this snippet
# simple_click is the command name
# = simple_click is the library name
# .cli is the cli.py file
# :cli is the cli function
#
# the second item is a shorthand alias to the main command

entry_points = [
   "simple_click = simple_click.cli:cli",
   "scli         = simple_click.cli:cli",
]


setup(
    name='simple_click',
    version='1.0.0',
    url='https://github.com/mypackage.git',
    packages=find_packages(),
    entry_points={"console_scripts": entry_points},

)

Enter fullscreen mode Exit fullscreen mode

🕶 See it in action

See it in action

📢 Discuss

What do You wish more python libraries included in their cli?

Shameless Plug

This page was originally posted on my personal blog https://waylonwalker.com/b/scli/

Top comments (3)

Collapse
 
artis3n profile image
Ari Kalfus

You mention that if you don't need a lot of tooling to just use argparse. What is click doing beyond argparse's capabilities?

Collapse
 
waylonwalker profile image
Waylon Walker

Click does not do anything that cannot be achieved from the standard library. Instead it makes composing cli applications simple. It automatically parses inputs to pass to your function. It automatically generates help text. There are also a number of plugins that make doing really powerful things very simple, for example click_did_you_mean will suggest possible commands if a subcommand does not exist with only one extra line of code.

From the docs

It aims to make the process of writing command line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API.

Click in three points:

arbitrary nesting of commands

automatic help page generation

supports lazy loading of subcommands at runtime

Collapse
 
artis3n profile image
Ari Kalfus

Thanks!