6.20.3.4 Option types

optparse supports six option types out of the box: string, int, long, choice, float and complex. (Of these, string, int, float, and choice are the most commonly used --long and complex are there mainly for completeness.) It's easy to add new option types by subclassing the Option class; see section 6.20.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 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 in optparse.check_builtin(); at a higher level (in OptionParser), optparse catches this exception 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. A master list or tuple of choices (strings) must be passed to the option constructor (make_option() or OptionParser.add_option()) as the choices keyword argument. Choice option arguments are compared against this master list in optparse.check_choice(), and OptionValueError is raised if an unknown string is given.

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