6.20.2.2 Other store_* actions

Flag options--set a variable to true or false when a particular option is seen--are quite common. optparse supports them with two separate actions, ``store_true'' and ``store_false''. For example, you might have a verbose flag that is turned on with -v and off with -q:

parser.add_option("-v", action="store_true", dest="verbose")
parser.add_option("-q", action="store_false", dest="verbose")

Here we have two different options with the same destination, which is perfectly OK. (It just means you have to be a bit careful when setting default values--see below.)

When optparse sees -v on the command line, it sets options.verbose to True; when it sees -q, it sets options.verbose to False.

See About this document... for information on suggesting changes.