Parsing Command Line arguments
Most command line tools rely on arguments passed to the program upon its execution. Instead of prompting for input, these programs expect data or specific flags (which become booleans) to be set. This allows both the user and other programs to run the Python file passing it data as it starts. This section explains and demonstrates the implementation and usage of command line arguments in Python.
Advanced example with docopt and docopt_dispatch
As with docopt, with [docopt_dispatch] you craft your
--help in the
__doc__ variable of your entry-point module. There, you call
dispatch with the doc string as argument, so it can run the parser over it.
That being done, instead of handling manually the arguments (which usually ends up in a high cyclomatic if/else structure), you leave it to dispatch giving only how you want to handle the set of arguments.
This is what the
dispatch.on decorator is for: you give it the argument or sequence of arguments that should trigger the function, and that function will be executed with the matching values as parameters.
Basic example with docopt
docopt turns command-line argument parsing on its head. Instead of parsing the arguments, you just write the usage string for your program, and docopt parses the usage string and uses it to extract the command line arguments.
Conceptual grouping of arguments with argparse.add_argument_group()
When you create an argparse ArgumentParser() and run your program with '-h' you get an automated usage message explaining what arguments you can run your software with. By default, positional arguments and conditional arguments are separated into two categories, for example, here is a small script (example.py) and the output when you run
python example.py -h.
There are some situations where you want to separate your arguments into further conceptual sections to assist your user. For example, you may wish to have all the input options in one group, and all the output formating options in another. The above example can be adjusted to separate the
--foo_* args from the
--bar_* args like so.
Which produces this output when
python example.py -h is run:
Custom parser error message with argparse
You can create parser error messages according to your script needs. This is through the
argparse.ArgumentParser.error function. The below example shows the script printing a usage and an error message to
--foo is given but not
Assuming your script name is sample.py, and we run:
python sample.py --foo ds_in_fridge
The script will complain with the following:
Hello world in argparse
The following program says hello to the user. It takes one positional argument, the name of the user, and can also be told the greeting.
$ python hello.py --help usage: hello.py [-h] [-g GREETING] name positional arguments: name name of user optional arguments: -h, --help show this help message and exit -g GREETING, --greeting GREETING optional alternate greeting
$ python hello.py world Hello, world! $ python hello.py John -g Howdy Howdy, John!
For more details please read the argparse documentation.
Setting mutually exclusive arguments with argparse
If you want two or more arguments to be mutually exclusive. You can use the function
argparse.ArgumentParser.add_mutually_exclusive_group(). In the example below, either foo or bar can exist but not both at the same time.
If you try to run the script specifying both
--bar arguments, the script will complain with the below message.
error: argument -b/--bar: not allowed with argument -f/--foo
Using command line arguments with argv
Whenever a Python script is invoked from the command line, the user may supply additional command line arguments which will be passed on to the script. These arguments will be available to the programmer from the system variable
sys.argv ("argv" is a traditional name used in most programming languages, and it means "argument vector").
By convention, the first element in the
sys.argv list is the name of the Python script itself, while the rest of the elements are the tokens passed by the user when invoking the script.
Here's another example of how to use
argv. We first strip off the initial element of sys.argv because it contains the script's name. Then we combine the rest of the arguments into a single sentence, and finally print that sentence prepending the name of the currently logged-in user (so that it emulates a chat program).
The algorithm commonly used when "manually" parsing a number of non-positional arguments is to iterate over the
sys.argv list. One way is to go over the list and pop each element of it: