There are several ways to populate the parser with options. One way is to pass a list of Options to the OptionParser constructor:
from optparse import OptionParser, make_option [...] parser = OptionParser(option_list=[ make_option("-f", "--filename", action="store", type="string", dest="filename"), make_option("-q", "--quiet", action="store_false", dest="verbose")])
(make_option() is a factory function for generating Option objects.)
For long option lists, it may be more convenient/readable to create the list separately:
option_list = [make_option("-f", "--filename", action="store", type="string", dest="filename"), [... more options ...] make_option("-q", "--quiet", action="store_false", dest="verbose")] parser = OptionParser(option_list=option_list)
Or, you can use the add_option() method of OptionParser to add options one-at-a-time:
parser = OptionParser() parser.add_option("-f", "--filename", action="store", type="string", dest="filename") parser.add_option("-q", "--quiet", action="store_false", dest="verbose")
This method makes it easier to track down exceptions raised by the Option constructor, which are common because of the complicated interdependencies among the various keyword arguments. (If you get it wrong, optparse raises OptionError.)
add_option() can be called in one of two ways:
See About this document... for information on suggesting changes.