Parsing command-line arguments in Python
Nothing to hand in, but you're going to want to know this stuff.
Goals
- Learn about the raw list of command-line arguments
sys.argvin Python - Learn about the standard Python module
argparseas one tool for parsing complex command lines. - Get links to a bunch more command-line parsing options in Python
Update your copy of my samples repository
If you don't have it yet:
If you do have it already:
Once you have gotten an updated clone of my repo, make sure it has a folder
named arguments with two python programs in it.
Simple CLI processing using sys.argv
In Python, the most rudimentary way to get information from command-line arguments
is by using the sys.argv list.
To get a feel for this, try creating a file named example.py and
put this code in it:
Try running this program with whatever arguments you want. For example:
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
In my repository, there's a
simple sample program called sysargv-sample.py
that uses sys.argv.
In your clone of my repo, cd to arguments 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.
- Think briefly about what modifications you would make if you wanted to let the user
optionally specify their age. (i.e.,
python3 sysargv-sample.py Jeff 61is OK, but so is justpython3 sysargv-sample.py Jeff)
CLI processing with argparse
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, look in my repo's arguments directory for this
sample argparse program called argparse-sample.py.
- 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
--yellor-y. If this option is present in the command line, the output should be all caps. (e.g.python3 argparse-sample.py dog -yshould produceThe 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.)