Parsing command-line arguments in Python
Nothing to hand in
Goals
- Learn about the raw list of command-line arguments sys.argv in Python
- Learn about the standard Python module argparse as one tool for parsing complex command lines.
- Get links to a bunch more command-line parsing options in Python
sys.argv
In Python, the most rudimentary way to get information from command-line arguments is by using the sys.argv list. Try running this program, for example:
Run this program with whatever arguments you want. If you save it as example.py, then try
The point is that sys.argv is a list of strings starting with the name of the program file and continuing with the rest of the space-delimited arguments from the command line.
Try this sys.argv-based sample
Here's a simple sample program that uses sys.argv. Grab a copy of it and try the following:
- Read the program and predict what it will do when you run it
- What modifications would you make if you wanted to let the user specify their age as a command-line argument? Give that a try.
- What modifications would you make if you wanted to let the user optionally specify their age? (i.e. "python3 sysargv-sample.py Jeff 61" is OK, but so is just "python3 sysargv-sample.py Jeff")
the argparse module
If your command-line syntax is at all complicated, using sys.argv is a major pain in the neck and also error-prone. As a result, people have written many Python modules to try to simplify command-line parsing. Two such modules, argparse and getopt, are included in the standard Python library, so they're always available on any Python installation. For your upcoming assignment, you will use the argparse module to implement your command-line interface.
Here's the official argparse documentation. It's packed with feature descriptions and sample code. They've also provided a tutorial if you want it.
To get started, grab a copy of my sample argparse program.
- Read it to get a feel for what it's intended to do.
- Run it as just "python3 argparse-sample.py". What do you see?
- Run it as "python3 argparse-sample.py -h". What do you see?
- Try running it with the various options specified in the usage statement. Does it work as expected?
- Try adding a new optional argument "--yell" or "-y". If this option is present in the command line, the output should be all caps. (e.g. "python3 argparse-sample.py dog -y" should produce "The dog says "WOOF"")
Other choices
There are many Python modules for command-line parsing. As is usual with programming tools, each of these modules has its disciples and detractors.
- argparse (part of the Python standard library)
- getopt (part of the Python standard library)
- docopt
- click
- typer
- fire
- ...probably more...
I don't really love any of the choices. I think that's because command-line parsing is inherently kind of a mess, so the modules that try to make it easier are also kind of a mess. But I find argparse tolerable and better for non-trivial command interfaces than sys.argv, and I happen to like argparse better than getopt, so I use argparse for a fair number of my Python programs.
The rest of the options are third-party libraries not included in the Python standard library. (Go ahead—ask me my feelings about third-party libraries sometime.)