On this page
optparse.OptionParser
Prev: optparse.OptionGroup | Up: optparse | Next: optparse.OptionValueError
Rel: optparse.BadOptionError, optparse.check_choice, optparse.HelpFormatter, optparse.IndentedHelpFormatter
Ref: docs.python.org — optparse.OptionParser
Tags: #public #python3 #stdlib #optparse
Import
import optparse
from optparse import OptionParser
Signature & Overview
optparse.OptionParser(usage=None, option_list=None, option_class=<class 'optparse.Option'>, version=None, conflict_handler='error', description=None, formatter=None, add_help_option=True, prog=None, epilog=None)
Class attributes: standard_option_list : [Option] list of standard options that will be accepted by all instances
Methods & Attributes
OptionParser.add_option(self, *args, **kwargs)— add_option(opt_str, ..., kwarg=val, ...)OptionParser.add_option_group(self, *args, **kwargs)—OptionParser.add_option_groupmethod/property.OptionParser.add_options(self, option_list)—OptionParser.add_optionsmethod/property.OptionParser.check_values(self, values, args)— -> (values : Values, args : [string]) Check that the supplied option values and leftover arguments are valid. Returns the option values and leftover argumentsOptionParser.destroy(self)— 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(), theOptionParser.disable_interspersed_args(self)— 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 optionsOptionParser.enable_interspersed_args(self)— 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 theOptionParser.error(self, msg)— Print a usage message incorporating 'msg' to stderr and exit.OptionParser.exit(self, status=0, msg=None)—OptionParser.exitmethod/property.OptionParser.expand_prog_name(self, s)—OptionParser.expand_prog_namemethod/property.OptionParser.format_description(self, formatter)—OptionParser.format_descriptionmethod/property.OptionParser.format_epilog(self, formatter)—OptionParser.format_epilogmethod/property.OptionParser.format_help(self, formatter=None)—OptionParser.format_helpmethod/property.OptionParser.format_option_help(self, formatter=None)—OptionParser.format_option_helpmethod/property.OptionParser.get_default_values(self)—OptionParser.get_default_valuesmethod/property.OptionParser.get_description(self)—OptionParser.get_descriptionmethod/property.OptionParser.get_option(self, opt_str)—OptionParser.get_optionmethod/property.OptionParser.get_option_group(self, opt_str)—OptionParser.get_option_groupmethod/property.OptionParser.get_prog_name(self)—OptionParser.get_prog_namemethod/property.OptionParser.get_usage(self)—OptionParser.get_usagemethod/property.OptionParser.get_version(self)—OptionParser.get_versionmethod/property.OptionParser.has_option(self, opt_str)—OptionParser.has_optionmethod/property.OptionParser.parse_args(self, args=None, values=None)— parse_args(args : [string] = sys.argv[1:], values : Values = None) -> (values : Values, args : [string])OptionParser.print_help(self, file=None)— Print an extended help message, listing all options and any help text provided with them, to 'file' (default stdout).OptionParser.print_usage(self, file=None)— 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 programOptionParser.print_version(self, file=None)— 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'sOptionParser.remove_option(self, opt_str)—OptionParser.remove_optionmethod/property.OptionParser.set_conflict_handler(self, handler)—OptionParser.set_conflict_handlermethod/property.OptionParser.set_default(self, dest, value)—OptionParser.set_defaultmethod/property.OptionParser.set_defaults(self, **kwargs)—OptionParser.set_defaultsmethod/property.OptionParser.set_description(self, description)—OptionParser.set_descriptionmethod/property.OptionParser.set_process_default_values(self, process)—OptionParser.set_process_default_valuesmethod/property.OptionParser.set_usage(self, usage)—OptionParser.set_usagemethod/property.
Usage
import optparse
p = optparse.OptionParser()
p.add_option("-n", "--name", default="world")
opts, args = p.parse_args(["--name", "Ada", "extra"])
assert opts.name == "Ada" and args == ["extra"]
Gotchas
- Deprecated: Returns
(options, positional_args)separately; useargparse.ArgumentParserfor new scripts.
REPL Inspect
from optparse import OptionParser
help(OptionParser)
Linked from
Related notes
- tuts/python3/stdlib_module/optparse/OptionGroup Linked here; links to this page; shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/OptionValueError Linked here; links to this page; shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/BadOptionError Linked here; shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/check_choice Linked here; shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/HelpFormatter Linked here; shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/IndentedHelpFormatter Linked here; shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/make_option shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/Option shared tags: #optparse, #stdlib
- tuts/python3/stdlib_module/optparse/OptionConflictError shared tags: #optparse, #stdlib