6.21.3.6 Standard option types
optparse has six built-in option types: string
, int
, long
,
choice
, float
and complex
. If you need to add new option
types, see sectionÂ
Arguments to string options are not checked or converted in any way: the text on the command line is stored in the destination (or passed to the callback) as-is.
Integer arguments are passed to int()
to convert them to Python
integers. If int()
fails, so will optparse, although with a more
useful error message. (Internally, optparse raises OptionValueError;
OptionParser catches this exception higher up and terminates your
program with a useful error message.)
Likewise, float
arguments are passed to float()
for conversion,
long
arguments to long()
, and complex
arguments to
complex()
. Apart from that, they are handled identically to integer
arguments.
choice
options are a subtype of string
options. The choices
option attribute (a sequence of strings) defines the set of allowed
option arguments. optparse.check_choice()
compares
user-supplied option arguments against this master list and raises
OptionValueError if an invalid string is given.
See About this document... for information on suggesting changes.