14.3.3.1 Creating the parser

The first step in using optparse is to create an OptionParser instance:

parser = OptionParser(...)

The OptionParser constructor has no required arguments, but a number of optional keyword arguments. You should always pass them as keyword arguments, i.e. do not rely on the order in which the arguments are declared.

usage (default: "%prog [options]")
The usage summary to print when your program is run incorrectly or with a help option. When optparse prints the usage string, it expands %prog to os.path.basename(sys.argv[0]) (or to prog if you passed that keyword argument). To suppress a usage message, pass the special value optparse.SUPPRESS_USAGE.
option_list (default: [])
A list of Option objects to populate the parser with. The options in option_list are added after any options in standard_option_list (a class attribute that may be set by OptionParser subclasses), but before any version or help options. Deprecated; use add_option() after creating the parser instead.
option_class (default: optparse.Option)
Class to use when adding options to the parser in add_option().
version (default: None)
A version string to print when the user supplies a version option. If you supply a true value for version, optparse automatically adds a version option with the single option string "-version". The substring "%prog" is expanded the same as for usage.
conflict_handler (default: "error")
Specifies what to do when options with conflicting option strings are added to the parser; see section 14.3.3, Conflicts between options.
description (default: None)
A paragraph of text giving a brief overview of your program. optparse reformats this paragraph to fit the current terminal width and prints it when the user requests help (after usage, but before the list of options).
formatter (default: a new IndentedHelpFormatter)
An instance of optparse.HelpFormatter that will be used for printing help text. optparse provides two concrete classes for this purpose: IndentedHelpFormatter and TitledHelpFormatter.
add_help_option (default: True)
If true, optparse will add a help option (with option strings "-h" and "-help") to the parser.
prog
The string to use when expanding "%prog" in usage and version instead of os.path.basename(sys.argv[0]).

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