A Python command line parser you can love

Day 2 - Systems Programming (Thu) • 14:30 • Duration: 45m

The behaviour of most programs, even those normally used via their GUI, can be changed by starting the program with some arguments, usually from the command line.

The interpretation of these arguments, command line (argument) parsing, is up to the program, but various libraries exist that provide both a standardised way of interpreting these arguments, as well as extra functionality, such as generation of nicely formatter help information.

In this talk I will first provide some terminology and give a examples of different assumptions in command line parsing, focussing on Unix utilities. I will describe how these influenced the standard library routines getopt, optparse and argparse, focussing on the latter and comparing that with alternatives such as click and docopt. Some of these libraries and enhancements thereof re-use, and extend, already specified information, such as function/method arguments.

I will touch on the related topics of late casting/interpretation of arguments. And also on (intelligent) command line completion, and problems for users of Python based command line programs.

For several years I used my home-brew, semi-declarative, way of specifying command line arguments by enhancing argparse. Then I switched to a fully configuration based system. I will describe its pros and cons of each, and how it was possible to largely automate that switch with example configurations. This system initially generated argparse code for the 200+ utilities it is used for and worked fine for a few years. Recently it was changed to generatie more simple Python code, this doubled the generated code size, but halved the startup time of my programs.

I will finish by describing further steps in generating code, including that of completions, and how Zig comes into play.

The code for this project is open source.