14.3.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 14.3.5, Extending optparse.
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 (type int
or long
) are parsed as follows:
-
if the number starts with
0x
, it is parsed as a hexadecimal number -
if the number starts with
0
, it is parsed as an octal number -
if the number starts with
0b
, is is parsed as a binary number -
otherwise, the number is parsed as a decimal number
The conversion is done by calling either int()
or long()
with
the appropriate base (2, 8, 10, or 16). If this fails, so will optparse,
although with a more useful error message.
float
and complex
option arguments are converted directly with
float()
and complex()
, with similar error-handling.
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.