PyDocs

Nav

Command-line interface libraries

OptionParser

  • OptionParser(...)
Class attributes:
  standard_option_list : [Option]
    list of standard options that will be accepted by all instances
    of this parser class (intended to be overridden by subclasses).

Instance attributes:
  usage : string
    a usage string for your program.  Before it is displayed
    to the user, "%prog" will be expanded to the name of
    your program (self.prog or os.path.basename(sys.argv[0])).
  prog : string
    the name of the current program (to override
    os.path.basename(sys.argv[0])).
  description : string
    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).
  epilog : string
    paragraph of help text to print after option help

  option_groups : [OptionGroup]
    list of option groups in this parser (option groups are
    irrelevant for parsing the command-line, but very useful
    for generating help)

  allow_interspersed_args : bool = true
    if true, positional arguments may be interspersed with options.
    Assuming -a and -b each take a single argument, the command-line
      -ablah foo bar -bboo baz
    will be interpreted the same as
      -ablah -bboo -- foo bar baz
    If this flag were false, that command line would be interpreted as
      -ablah -- foo bar -bboo baz
    -- ie. we stop processing options as soon as we see the first
    non-option argument.  (This is the tradition followed by
    Python's getopt module, Perl's Getopt::Std, and other argument-
    parsing libraries, but it is generally annoying to users.)

  process_default_values : bool = true
    if true, option default values are processed similarly to option
    values from the command line: that is, they are passed to the
    type-checking function for the option's type (as long as the
    default value is a string).  (This really only matters if you
    have defined custom types; see SF bug #955889.)  Set it to false
    to restore the behaviour of Optik 1.4.1 and earlier.

  rargs : [string]
    the argument list currently being parsed.  Only set when
    parse_args() is active, and continually trimmed down as
    we consume arguments.  Mainly there for the benefit of
    callback options.
  largs : [string]
    the list of leftover arguments that we have skipped while
    parsing options.  If allow_interspersed_args is false, this
    list is always empty.
  values : Values
    the set of option values currently being accumulated.  Only
    set when parse_args() is active.  Also mainly for callbacks.

Because of the 'rargs', 'largs', and 'values' attributes,
OptionParser is not thread-safe.  If, for some perverse reason, you
need to parse command-line arguments simultaneously in different
threads, use different OptionParser instances.
Members of OptionParser

a

add_option
add_option(opt_str, ..., kwarg=val, ...)

d

disable_interspersed_args
Set parsing to stop on the first non-option.

e

enable_interspersed_args
Set parsing to not stop on the first non-option, allowing interspersing switches with command arguments.

g

get_option
Returns the Option instance with the option string opt_str, or None if no options have that option string.
get_option_group
Return the OptionGroup to which the short or long option string opt_str (e.g.
get_usage
Same as print_usage() but returns the usage string instead of printing it.
get_version
Same as print_version() but returns the version string instead of printing it.

h

has_option
Return True if the OptionParser has an option with option string opt_str (e.g., -q or --verbose).

p

parse_args
parse_args(args : [string] = sys.argv[1:], values : Values = None) -> (values : Values, args : [string])
print_usage
Print the usage message for the current program (self.usage) to 'file' (default stdout).
print_version
Print the version message for this program (self.version) to 'file' (default stdout).

r

remove_option
If the OptionParser has an option corresponding to opt_str, that option is removed.

s

set_defaults
Set default values for several option destinations at once.
set_usage
Set the usage string according to the rules described above for the usage constructor keyword argument.
0 Examples
No examples for optparse/OptionParser.
See Also
No see-alsos for optparse/OptionParser
0 Notes
No notes for OptionParser