6.21.3.10 Other methods

Python 2.2

6.21.3.10 Other methods

OptionParser supports several other public methods:

  • set_usage(usage)

    Set the usage string according to the rules described above for the usage constructor keyword argument. Passing None sets the default usage string; use SUPPRESS_USAGE to suppress a usage message.

  • enable_interspersed_args(), disable_interspersed_args()

    Enable/disable positional arguments interspersed with options, similar to GNU getopt (enabled by default). For example, if "-a" and "-b" are both simple options that take no arguments, optparse normally accepts this syntax:

    prog -a arg1 -b arg2
    

    and treats it as equivalent to

    prog -a -b arg1 arg2
    

    To disable this feature, call disable_interspersed_args(). This restores traditional Unix syntax, where option parsing stops with the first non-option argument.

  • set_defaults(dest=value, ...)

    Set default values for several option destinations at once. Using set_defaults() is the preferred way to set default values for options, since multiple options can share the same destination. For example, if several ``mode'' options all set the same destination, any one of them can set the default, and the last one wins:

    parser.add_option("--advanced", action="store_const",
                      dest="mode", const="advanced",
                      default="novice")    # overridden below
    parser.add_option("--novice", action="store_const",
                      dest="mode", const="novice",
                      default="advanced")  # overrides above setting
    

    To avoid this confusion, use set_defaults():

    parser.set_defaults(mode="advanced")
    parser.add_option("--advanced", action="store_const",
                      dest="mode", const="advanced")
    parser.add_option("--novice", action="store_const",
                      dest="mode", const="novice")
    

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