This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

External CLI libraries

argparse is a pretty powerful built-in Python module. But there are also external libraries that makes your life even easier!

plac

plac is an external CLI library for the lazy people (like me!). I use this quite a lot after discovering its existence!

Essentially, plac wraps over argparse and hides all the complexity by inferring the argument parser, rather than requiring you to specify these by hand.

Because it is not in the Python Standard library, you will first need to install it with pip install plac.

Here is how you use plac (I’ll call our script lazy.py):

import plac

def calculate(x, y, operation="add"):
    """ Take two numbers and perform an operation.
    """

    if operation == "add":
        return x + y
    elif operation == "sub":
        return x - y
    elif operation == "mul":
        return x * y
    elif operation == "div":
        return x / y

if __name__ == "__main__":
    plac.call(calculate)

And when you run lazy.py, magic happens! 🤯

$ python3 lazy.py -h
usage: lazy.py [-h] x y operation

Take two numbers and perform an operation.

positional arguments:
  x
  y
  operation   [add]

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

So, you have a CLI program with hardly any effort on your part! It even shows you that the default value for operation is “add”, which is what you have in your function definition. 🤯 And if you have included a docstring in your function (you have been documenting your functions, haven’t you?), this will be in the description as well.

If you put just a bit more effort, then you can be even more descriptive:

import plac

@plac.pos("x", "The first number to use in the calculation", type=int)
@plac.pos("y", "The second number to use in the calculation", type=int)
@plac.pos("operation", "The operation to perform on x and y.", choices=["add", "sub", "mul", "div"])
@plac.opt("base", "The base of the output", type=int)
def calculate(x, y, operation="add", base=10):
    """ Take two numbers and perform an operation.
    """

    if operation == "add":
        return x + y
    elif operation == "sub":
        return x - y
    elif operation == "mul":
        return x * y
    elif operation == "div":
        return x / y

if __name__ == "__main__":
    plac.call(calculate)
$ python3 lazy.py -h
usage: lazy.py [-h] [-b 10] x y [{add,sub,mul,div}]

Take two numbers and perform an operation.

positional arguments:
  x                  The first number to use in the calculation
  y                  The second number to use in the calculation
  {add,sub,mul,div}  The operation to perform on x and y.

optional arguments:
  -h, --help         show this help message and exit
  -b 10, --base 10   The base of the output


$ python3 lazy.py 1
usage: lazy.py [-h] [-b 10] x y [{add,sub,mul,div}]
lazy.py: error: the following arguments are required: y

You can even integrate your documentation inside your code, using function annotations in Python 3 (this is something you have briefly seen in your first coursework).

import plac

def calculate(x: "The first number to use in the calculation", 
              y: "The second number to use in the calcuation", 
              operation: "The operation to perform on x and y" = "add", 
              base: ("The base of the output", "option", "base") = 10):
    """ Take two numbers and perform an operation.
    """

    if operation == "add":
        return x + y
    elif operation == "sub":
        return x - y
    elif operation == "mul":
        return x * y
    elif operation == "div":
        return x / y

if __name__ == "__main__":
    plac.call(calculate)

Have I sold plac to you yet? (Why did we even bother with argparse? 😅)

Please see the official web page if you want to see more examples and features of plac.

Other external CLI libraries

Here are a few more external libraries if you are not sold on plac and want to hunt around further: