How to write CLIs for applications such as in Bash or Python.

Tools

Listed here by programming language.

Python

There are other Python libraries (comparison article), but these are the popular modern versions.

  • argparse
  • click
    • Click versions on Read The Docs with a choice of versions.
    • Click docs for master.
    • Why Click?
    • Install using pip install click.

Bash

Examples

Argparse

What the usage and help sections can appear - here using Python argparse.

Positional arguments

python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

optional arguments:
 -h, --help  show this help message and exit

Note that is help is included as an optional argument by default.

Optional arguments

Here is a flag with a value.

python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO   foo help

Groups

usage: PROG [--bar BAR] foo

group1:
  group1 description

  foo    foo help

group2:
  group2 description

  --bar BAR  bar help

Subcommands

usage:  [-h] {foo,bar} ...

optional arguments:
  -h, --help  show this help message and exit

subcommands:
  valid subcommands

  {foo,bar}   additional help

Click

Positional arguments

Usage: hello.py [OPTIONS] FOO

Optional arguments

Usage: hello.py [OPTIONS]

  Simple program that greets NAME for a total of COUNT times.

Options:
  --count INTEGER  Number of greetings.
  --name TEXT      The person to greet.
  --help           Show this message and exit.

Usage guidelines

Help on writing usage instructions, especially when writing by hand.

List of items

Zero or more items:

command [ITEM [ITEM ...]]

One or more items:

command ITEM [ITEM ...]

Often a comma is used in these usage instructions but it can be misleading if a comma is not needed in the literal answer.