This article was co-written with Lacey Williams Henschel[1].

Sometimes the right tool for the job is a command-line application. A command-line application is a program that you interact with and run from something like your shell or Terminal. Git[2] and Curl[3] are examples of command-line applications that you might already be familiar with.

Command-line apps are useful when you have a bit of code you want to run several times in a row or on a regular basis. Django developers run commands like ./manage.py runserver to start their web servers; Docker developers run docker-compose up to spin up their containers. The reasons you might want to write a command-line app are as varied as the reasons you might want to write code in the first place.

For this month's Python column, we have three libraries to recommend to Pythonistas looking to write their own command-line tools.

Click

Click[4] is our favorite Python package for command-line applications. It:

  • Has great documentation filled with examples
  • Includes instructions on packaging your app as a Python application so it's easier to run
  • Automatically generates useful help text
  • Lets you stack optional and required arguments and even several commands[5]
  • Has a Django version (django-click[6]) for writing management commands

Click uses its @click.command() to declare a function as a command and specify required or optional arguments.


# hello.py
import click

@click.command()
@click.option('--name', default='', help='Your name')
def say_hello(name):
    click.echo("Hello {}!".format

Read more from our friends at Opensource.com