Checking options with optparse callbacks
optparse is a flexible and powerful module for processing command line options in Python programs. I’ve used it for a while, but I didn’t have much occasion to get into callbacks until recently. A callback here is a user-defined function optparse calls when its associated option is encountered. They can help you keep your script’s logic clear within a forest of options by providing arbitrary checks when particular options are passed.
When I first began using them, I ran into a critical point of confusion that wasn’t clearly illuminated in the assorted guides I found for optparse. In response, here is a short overview of how to use callbacks to perform checks and validations against passed options.
First, a very basic script using optparse for option parsing:
1 2 3 4 5 6 7 | import optparse parser = optparse.OptionParser() parser.add_option("-f", "--input-file", dest="inputfile", help="The file with input") (options, arguments) = parser.parse_args() print options |
Which outputs:
1 2 | python test.py -f test.txt {'inputfile': 'test.txt'} |
For a more detailed coverage of the basics, review Doug Hellman’s PyMOTW article on optparse. It would be best to verify that this input file exists so that we might inform the user and exit early. Adding that functionality to our example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import optparse import os def check_file(option, opt, value, parser): if not os.path.exists(value): raise optparse.OptionValueError("%s doesn't seem to exist." % value) parser.values.inputfile = value parser = optparse.OptionParser() parser.add_option("-f", "--input-file", dest="inputfile", action="callback", callback=check_file, type="string", help="The file with input") (options, arguments) = parser.parse_args() print options |
We’ve added a check_file() function to perform our check and also added the callback specification to the associated option. The arguments a callback function is passed are described in the module documentation. optparse.OptionValueError provides an elegant way to send an error message about the problem and quit the program in one step.
So what’s the problem? When I first wrote a callback, I didn’t catch the line in the tutorial examples that set the value of the option. I still don’t understand why this is necessary, but without the “parser.values.inputfile = value” line above, you’d find the inputfile variable to have None for its value. Strange, but true. I could understand the callback function having to return the option value if all was well, but why would the option’s value get unset in the first place?
Additionally, it turns out you have to set the option’s type even if the desired value is supposed to be default. In the above example, without ‘type=”string”‘, the callback function would have thrown an error, complaining that we were claiming to use a string and weren’t.
So watch out for these details, and clean up your scripts with callbacks.
Possibly Related (no promises):
- A better way to search for methods of Python objects
- A better Ruby prompt
- Useful ways to list directory contents
- Useful Bash functions to determine OS and more
- Useful grep incantations
Related posts brought to you by Yet Another Related Posts Plugin.
August 16, 2009 - 12:44 AM







