On this page
optparse
Prev: operator | Up: stdlib_module | Next: os
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — optparse
Tags: #public #python3 #stdlib
Import
import optparse
Overview
Deprecated command-line option parser (OptionParser), superseded by argparse since Python 3.2. You'll still encounter optparse in legacy scripts and older build tools; use this page to read or maintain existing optparse code, and choose argparse for anything new.
Quick Start
import optparse
parser = optparse.OptionParser(usage="usage: %prog [options] file")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False)
parser.add_option("-n", "--count", type="int", default=1)
options, args = parser.parse_args(["-v", "-n", "3", "data.txt"])
assert options.verbose is True and options.count == 3 and args == ["data.txt"]
Implementation Walkthrough
1. Migrating optparse to argparse
optparse returns (options, positional_args) separately, whereas argparse unifies options and positional arguments on a single Namespace:
# optparse:
# parser.add_option("-o", "--output", dest="output", default="out.txt")
# opts, args = parser.parse_args()
#
# argparse equivalent:
# parser.add_argument("-o", "--output", default="out.txt")
# parser.add_argument("files", nargs="*")
# args = parser.parse_args()
Gotchas & Boundaries
- Deprecated:
optparseis frozen and receives no new features (no subcommands, nonargs="+", no positional argument validation). - String type names:
optparsetakestype="int"(a string), whereas argparse takestype=int(a callable). - No built-in subcommands: Implementing
git-style subcommands inoptparserequires manual dispatch; switch toargparse.add_subparsers().
Common Functions
- optparse.check_choice() —
check_choice(option, opt, value)—optparse.check_choice()function.
Classes & Objects
- optparse.BadOptionError — Raised if an invalid option is seen on the command line. (Methods:
.add_note(),.with_traceback()) - optparse.HelpFormatter — Abstract base class for formatting option help. OptionParser instances should use one of the HelpFormatter subclasses for formatting help; by default IndentedHelpFormatter is used. (Methods:
.dedent(),.expand_default(),.format_description(),.format_epilog(),.format_heading(),.format_option()) - optparse.IndentedHelpFormatter — Format help with indented section bodies. (Methods:
.dedent(),.expand_default(),.format_description(),.format_epilog(),.format_heading(),.format_option()) - optparse.make_option — Instance attributes: _short_opts : [string] _long_opts : [string] (Methods:
.check_value(),.convert_value(),.get_opt_string(),.process(),.take_action(),.takes_value()) - optparse.Option — Instance attributes: _short_opts : [string] _long_opts : [string] (Methods:
.check_value(),.convert_value(),.get_opt_string(),.process(),.take_action(),.takes_value()) - optparse.OptionConflictError — Raised if conflicting options are added to an OptionParser. (Methods:
.add_note(),.with_traceback()) - optparse.OptionContainer — Abstract base class. (Methods:
.add_option(),.add_options(),.destroy(),.format_description(),.format_help(),.format_option_help()) - optparse.OptionError — Raised if an Option instance is created with invalid or inconsistent arguments. (Methods:
.add_note(),.with_traceback()) - optparse.OptionGroup —
optparse.OptionGroupclass. (Methods:.add_option(),.add_options(),.destroy(),.format_description(),.format_help(),.format_option_help()) - optparse.OptionParser — Class attributes: standard_option_list : [Option] list of standard options that will be accepted by all instances (Methods:
.add_option(),.add_option_group(),.add_options(),.check_values(),.destroy(),.disable_interspersed_args()) - optparse.OptionValueError — Raised if an invalid option value is encountered on the command line. (Methods:
.add_note(),.with_traceback()) - optparse.OptParseError —
optparse.OptParseErrorclass. (Methods:.add_note(),.with_traceback()) - optparse.TitledHelpFormatter — Format help with underlined section headers. (Methods:
.dedent(),.expand_default(),.format_description(),.format_epilog(),.format_heading(),.format_option()) - optparse.Values —
optparse.Valuesclass. (Methods:.ensure_value(),.read_file(),.read_module())
Constants & Attributes
optparse.SUPPRESS_HELP(str) ='SUPPRESSHELP'optparse.SUPPRESS_USAGE(str) ='SUPPRESSUSAGE'
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/argparse
- tuts/python3/stdlib_module/getopt
- tuts/python3/stdlib_module/operator
- tuts/python3/stdlib_module/optparse/BadOptionError
- tuts/python3/stdlib_module/optparse/check_choice
- tuts/python3/stdlib_module/optparse/HelpFormatter
- tuts/python3/stdlib_module/optparse/IndentedHelpFormatter
- tuts/python3/stdlib_module/optparse/make_option
- tuts/python3/stdlib_module/optparse/Option
- tuts/python3/stdlib_module/optparse/OptionConflictError
- tuts/python3/stdlib_module/optparse/OptionContainer
- tuts/python3/stdlib_module/optparse/OptionError
- tuts/python3/stdlib_module/optparse/OptionGroup
- tuts/python3/stdlib_module/optparse/OptionParser
- tuts/python3/stdlib_module/optparse/OptionValueError
- tuts/python3/stdlib_module/optparse/OptParseError
- tuts/python3/stdlib_module/optparse/TitledHelpFormatter
- tuts/python3/stdlib_module/optparse/Values
- tuts/python3/stdlib_module/os
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/argparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/BadOptionError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/check_choice Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/HelpFormatter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/IndentedHelpFormatter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/make_option Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/Option Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse/OptionConflictError Linked here; links to this page; shared tags: #stdlib