All of the above examples involve setting some variable (the
``destination'') when certain command-line options are seen. What
happens if those options are never seen? Since we didn't supply any
defaults, they are all set to None. Sometimes, this is just fine (which
is why it's the default), but sometimes, you want more control. To
address that need, optparse lets you supply a default value for
each destination, which is assigned before the command-line is parsed.
First, consider the verbose/quiet example. If we want
optparse to set verbose to True unless
-q is seen, then we can do this:
parser.add_option("-v", action="store_true", dest="verbose", default=True)
parser.add_option("-q", action="store_false", dest="verbose")
Oddly enough, this is exactly equivalent:
parser.add_option("-v", action="store_true", dest="verbose")
parser.add_option("-q", action="store_false", dest="verbose", default=True)
Those are equivalent because you're supplying a default value for the option's destination, and these two options happen to have the same destination (the verbose variable).
Consider this:
parser.add_option("-v", action="store_true", dest="verbose", default=False)
parser.add_option("-q", action="store_false", dest="verbose", default=True)
Again, the default value for verbose will be True: the last
default value supplied for any particular destination is the one that
counts.
See About this document... for information on suggesting changes.