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
– 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.)
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.
Methods
add_option(Option) add_option(opt_str, ...) | |
add_option_group(*args, **kwargs) | |
add_options(option_list) | |
check_values(values : Values, ...) | -> (values : Values, args : [string]) |
destroy() | Declare that you are done with this OptionParser. This cleans up |
disable_interspersed_args() | Set parsing to stop on the first non-option. |
enable_interspersed_args() | Set parsing to not stop on the first non-option, allowing interspersing switches with command arguments. |
error(msg : string) | Print a usage message incorporating ‘msg’ to stderr and exit. |
exit([status, msg]) | |
expand_prog_name(s) | |
format_description(formatter) | |
format_epilog(formatter) | |
format_help([formatter]) | |
format_option_help([formatter]) | |
get_default_values() | |
get_description() | |
get_option(opt_str) | |
get_option_group(opt_str) | |
get_prog_name() | |
get_usage() | |
get_version() | |
has_option(opt_str) | |
parse_args(args : [string] = sys.argv[1:], ...) | Parse the command-line options found in ‘args’ (default: sys.argv[1:]). |
print_help(file : file = stdout) | Print an extended help message, listing all options and any |
print_usage(file : file = stdout) | Print the usage message for the current program (self.usage) to ‘file’ (default stdout). |
print_version(file : file = stdout) | Print the version message for this program (self.version) to ‘file’ (default stdout). |
remove_option(opt_str) | |
set_conflict_handler(handler) | |
set_default(dest, value) | |
set_defaults(**kwargs) | |
set_description(description) | |
set_process_default_values(process) | |
set_usage(usage) |
-> (values : Values, args : [string])
Check that the supplied option values and leftover arguments are valid. Returns the option values and leftover arguments (possibly adjusted, possibly completely new – whatever you like). Default implementation just returns the passed-in values; subclasses may override as desired.
Declare that you are done with this OptionParser. This cleans up reference cycles so the OptionParser (and all objects referenced by it) can be garbage-collected promptly. After calling destroy(), the OptionParser is unusable.
Set parsing to stop on the first non-option. Use this if you have a command processor which runs another command that has options of its own and you want to make sure these options don’t get confused.
Set parsing to not stop on the first non-option, allowing interspersing switches with command arguments. This is the default behavior. See also disable_interspersed_args() and the class documentation description of the attribute allow_interspersed_args.
Print a usage message incorporating ‘msg’ to stderr and exit. If you override this in a subclass, it should not return – it should either exit or raise an exception.
Parse the command-line options found in ‘args’ (default: sys.argv[1:]). Any errors result in a call to ‘error()’, which by default prints the usage message to stderr and calls sys.exit() with an error message. On success returns a pair (values, args) where ‘values’ is an Values instance (with all your option values) and ‘args’ is the list of arguments left over after parsing options.
Print an extended help message, listing all options and any help text provided with them, to ‘file’ (default stdout).
Print the usage message for the current program (self.usage) to ‘file’ (default stdout). Any occurrence of the string “%prog” in self.usage is replaced with the name of the current program (basename of sys.argv[0]). Does nothing if self.usage is empty or not defined.
Print the version message for this program (self.version) to ‘file’ (default stdout). As with print_usage(), any occurrence of “%prog” in self.version is replaced by the current program’s name. Does nothing if self.version is empty or undefined.