The various option actions all have slightly different requirements andeffects. Most actions have several relevant option attributes which youmay specify to guideoptparse's behaviour; a few have required attributes,which you must specify for any option using that action.
store [relevant:type,dest,nargs,choices]The option must be followed by an argument, which isconverted to a value according totype and stored indest. Ifnargs > 1, multiple arguments will be consumedfrom the command line; all will be converted according totype and stored todest as a tuple. See the ``Optiontypes'' section below.
Ifchoices is supplied (a list or tuple of strings), the typedefaults tochoice.
Iftype is not supplied, it defaults tostring.
Ifdest is not supplied,optparse derives a destination from thefirst long option string (e.g.,--foo-bar impliesfoo_bar).If there are no long option strings,optparse derives a destination fromthe first short option string (e.g.,"-f" impliesf).
Example:
parser.add_option("-f")parser.add_option("-p", type="float", nargs=3, dest="point")As it parses the command line
-f foo.txt -p 1 -3.5 4 -fbar.txt
optparse will set
options.f = "foo.txt"options.point = (1.0, -3.5, 4.0)options.f = "bar.txt"
store_const [required:const; relevant:dest]The valueconst is stored indest.
Example:
parser.add_option("-q", "--quiet", action="store_const", const=0, dest="verbose")parser.add_option("-v", "--verbose", action="store_const", const=1, dest="verbose")parser.add_option("--noisy", action="store_const", const=2, dest="verbose")If--noisy is seen,optparse will set
options.verbose = 2
store_true [relevant:dest]A special case ofstore_const that stores a true valuetodest.
store_false [relevant:dest]Likestore_true, but stores a false value.
Example:
parser.add_option("--clobber", action="store_true", dest="clobber")parser.add_option("--no-clobber", action="store_false", dest="clobber")append [relevant:type,dest,nargs,choices]The option must be followed by an argument, which is appended to thelist indest. If no default value fordest is supplied, anempty list is automatically created whenoptparse first encounters thisoption on the command-line. Ifnargs > 1, multiple arguments areconsumed, and a tuple of lengthnargs is appended todest.
The defaults fortype anddest are the same as for thestore action.
Example:
parser.add_option("-t", "--tracks", action="append", type="int")If"-t3" is seen on the command-line,optparse does the equivalent of:
options.tracks = []options.tracks.append(int("3"))If, a little later on,--tracks=4 is seen, it does:
options.tracks.append(int("4"))append_const [required:const; relevant:dest]Likestore_const, but the valueconst is appended todest;as withappend,dest defaults toNone, and an empty list isautomatically created the first time the option is encountered.
count [relevant:dest]Increment the integer stored atdest. If no default value issupplied,dest is set to zero before being incremented the firsttime.
Example:
parser.add_option("-v", action="count", dest="verbosity")The first time"-v" is seen on the command line,optparse does theequivalent of:
options.verbosity = 0options.verbosity += 1
Every subsequent occurrence of"-v" results in
options.verbosity += 1
callback [required:callback;relevant:type,nargs,callback_args,callback_kwargs]Call the function specified bycallback, which is called as
func(option, opt_str, value, parser, *args, **kwargs)
See section 14.3.4, Option Callbacks for more detail.
Prints a complete help message for all the options in thecurrent option parser. The help message is constructed fromtheusage string passed to OptionParser's constructor andthehelp string passed to every option.
If nohelp string is supplied for an option, it will still belisted in the help message. To omit an option entirely, usethe special valueoptparse.SUPPRESS_HELP.
optparse automatically adds ahelp option to all OptionParsers, soyou do not normally need to create one.
Example:
from optparse import OptionParser, SUPPRESS_HELPparser = OptionParser()parser.add_option("-h", "--help", action="help"),parser.add_option("-v", action="store_true", dest="verbose", help="Be moderately verbose")parser.add_option("--file", dest="filename", help="Input file to read data from"),parser.add_option("--secret", help=SUPPRESS_HELP)Ifoptparse sees eitherh or--help on the command line, itwill print something like the following help message to stdout(assumingsys.argv[0] is"foo.py"):
usage: foo.py [options]options: -h, --help Show this help message and exit -v Be moderately verbose --file=FILENAME Input file to read data from
After printing the help message,optparse terminates your processwithsys.exit(0).
versionPrints the version number supplied to the OptionParser to stdout andexits. The version number is actually formatted and printed by theprint_version() method of OptionParser. Generally only relevantif theversion argument is supplied to the OptionParserconstructor. As withhelp options, you will rarely createversion options, sinceoptparse automatically adds them when needed.