-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-126180: Remove getopt and optparse deprecation notices #126227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ncoghlan
merged 21 commits into
python:main
from
ncoghlan:reverse-getopt-optparse-deprecations
Dec 23, 2024
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a560986
Remove getopt and optparse deprecation notices
ncoghlan 0a568f1
Wording tweaks
ncoghlan 55b7215
Incorporate improvements from Serhiy's PR
ncoghlan 0971780
Grammar fix
ncoghlan 09b0237
Fix issue number reference
ncoghlan 4cdebde
Another wording tweak
ncoghlan 693a979
Update heading and description for new chapter
ncoghlan 50d1ee4
fileinput belongs in the new chapter
ncoghlan 4e50c74
Move getopt back to the superseded section
ncoghlan 0db2c88
Reword superseded chapter intro
ncoghlan e474e77
Simplify wording
ncoghlan 43234de
Merge remote-tracking branch 'origin/main' into reverse-getopt-optpar…
ncoghlan 93d7161
Updates based on Serhiy's feedback
ncoghlan bb91b52
Also reword What's New entry
ncoghlan 7cc72ad
Consistent getopt markup
ncoghlan 7f51456
Merge remote-tracking branch 'origin/main' into reverse-getopt-optpar…
ncoghlan 5d86774
Updates based on review feedback
ncoghlan c2ae293
Syntax fix
ncoghlan 5e2fcd9
Fix excess indentation in examples
ncoghlan 457ee88
Phrasing tweak
ncoghlan f67d20c
Merge branch 'main' into reverse-getopt-optparse-deprecations
ncoghlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.. _cmdlinelibs: | ||
|
||
******************************** | ||
Command Line Interface Libraries | ||
******************************** | ||
|
||
The modules described in this chapter assist with implementing | ||
command line and terminal interfaces for applications. | ||
|
||
Here's an overview: | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
argparse.rst | ||
optparse.rst | ||
getpass.rst | ||
fileinput.rst | ||
curses.rst | ||
curses.ascii.rst | ||
curses.panel.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ in this chapter is: | |
|
||
pathlib.rst | ||
os.path.rst | ||
fileinput.rst | ||
stat.rst | ||
filecmp.rst | ||
tempfile.rst | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,135 @@ | |
|
||
.. module:: optparse | ||
:synopsis: Command-line option parsing library. | ||
:deprecated: | ||
|
||
.. moduleauthor:: Greg Ward <[email protected]> | ||
.. sectionauthor:: Greg Ward <[email protected]> | ||
|
||
**Source code:** :source:`Lib/optparse.py` | ||
|
||
.. deprecated:: 3.2 | ||
The :mod:`optparse` module is :term:`soft deprecated` and will not be | ||
developed further; development will continue with the :mod:`argparse` | ||
module. | ||
|
||
-------------- | ||
|
||
.. _choosing-an-argument-parser: | ||
|
||
Choosing an argument parsing library | ||
------------------------------------ | ||
|
||
The standard library includes three argument parsing libraries: | ||
|
||
* :mod:`getopt`: a module that closely mirrors the procedural C ``getopt`` API. | ||
Included in the standard library since before the initial Python 1.0 release. | ||
* :mod:`optparse`: a declarative replacement for ``getopt`` that | ||
provides equivalent functionality without requiring each application | ||
to implement its own procedural option parsing logic. Included | ||
in the standard library since the Python 2.3 release. | ||
* :mod:`argparse`: a more opinionated alternative to ``optparse`` that | ||
provides more functionality by default, at the expense of reduced application | ||
flexibility in controlling exactly how arguments are processed. Included in | ||
the standard library since the Python 2.7 and Python 3.2 releases. | ||
|
||
In the absence of more specific argument parsing design constraints, :mod:`argparse` | ||
is the recommended choice for implementing command line applications, as it offers | ||
the highest level of baseline functionality with the least application level code. | ||
|
||
:mod:`getopt` is retained almost entirely for backwards compatibility reasons. | ||
However, it also serves a niche use case as a tool for prototyping and testing | ||
command line argument handling in ``getopt``-based C applications. | ||
|
||
:mod:`optparse` should be considered as an alternative to :mod:`argparse` in the | ||
following cases: | ||
|
||
* an application is already using :mod:`optparse` and doesn't want to risk the | ||
subtle behavioural changes that may arise when migrating to :mod:`argparse` | ||
* the application requires additional control over the way options and | ||
positional parameters are interleaved on the command line (including | ||
the ability to disable the interleaving feature completely) | ||
* the application requires additional control over the incremental parsing | ||
of command line elements (while ``argparse`` does support this, the | ||
exact way it works in practice is undesirable for some use cases) | ||
* the application requires additional control over the handling of options | ||
which accept parameter values that may start with ``-`` (such as delegated | ||
options to be passed to invoked subprocesses) | ||
* the application requires some other command line parameter processing | ||
behavior which ``argparse`` does not support, but which can be implemented | ||
in terms of the lower level interface offered by ``optparse`` | ||
|
||
These considerations also mean that :mod:`optparse` is likely to provide a | ||
better foundation for library authors writing third party command line | ||
argument processing libraries. | ||
|
||
As a concrete example, consider the following two command line argument | ||
parsing configurations, the first using ``optparse``, and the second | ||
using ``argparse``: | ||
|
||
.. testcode:: | ||
|
||
import optparse | ||
|
||
if __name__ == '__main__': | ||
parser = optparse.OptionParser() | ||
parser.add_option('-o', '--output') | ||
parser.add_option('-v', dest='verbose', action='store_true') | ||
opts, args = parser.parse_args() | ||
process(args, output=opts.output, verbose=opts.verbose) | ||
|
||
.. testcode:: | ||
|
||
import argparse | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-o', '--output') | ||
parser.add_argument('-v', dest='verbose', action='store_true') | ||
parser.add_argument('rest', nargs='*') | ||
args = parser.parse_args() | ||
process(args.rest, output=args.output, verbose=args.verbose) | ||
|
||
The most obvious difference is that in the ``optparse`` version, the non-option | ||
arguments are processed separately by the application after the option processing | ||
is complete. In the ``argparse`` version, positional arguments are declared and | ||
processed in the same way as the named options. | ||
|
||
However, the ``argparse`` version will also handle some parameter combination | ||
differently from the way the ``optparse`` version would handle them. | ||
For example (amongst other differences): | ||
|
||
* supplying ``-o -v`` gives ``output="-v"`` and ``verbose=False`` | ||
when using ``optparse``, but a usage error with ``argparse`` | ||
(complaining that no value has been supplied for ``-o/--output``, | ||
since ``-v`` is interpreted as meaning the verbosity flag) | ||
* similarly, supplying ``-o --`` gives ``output="--"`` and ``args=()`` | ||
when using ``optparse``, but a usage error with ``argparse`` | ||
(also complaining that no value has been supplied for ``-o/--output``, | ||
since ``--`` is interpreted as terminating the option processing | ||
and treating all remaining values as positional arguments) | ||
* supplying ``-o=foo`` gives ``output="=foo"`` when using ``optparse``, | ||
but gives ``output="foo"`` with ``argparse`` (since ``=`` is special | ||
cased as an alternative separator for option parameter values) | ||
|
||
Whether these differing behaviors in the ``argparse`` version are | ||
considered desirable or a problem will depend on the specific command line | ||
application use case. | ||
|
||
.. seealso:: | ||
|
||
:pypi:`click` is a third party argument processing library (originally | ||
based on ``optparse``), which allows command line applications to be | ||
developed as a set of decorated command implementation functions. | ||
|
||
Other third party libraries, such as :pypi:`typer` or :pypi:`msgspec-click`, | ||
allow command line interfaces to be specified in ways that more effectively | ||
integrate with static checking of Python type annotations. | ||
|
||
|
||
Introduction | ||
------------ | ||
|
||
:mod:`optparse` is a more convenient, flexible, and powerful library for parsing | ||
command-line options than the old :mod:`getopt` module. :mod:`optparse` uses a | ||
more declarative style of command-line parsing: you create an instance of | ||
:class:`OptionParser`, populate it with options, and parse the command | ||
line. :mod:`optparse` allows users to specify options in the conventional | ||
command-line options than the minimalist :mod:`getopt` module. | ||
gpshead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:mod:`optparse` uses a more declarative style of command-line parsing: | ||
you create an instance of :class:`OptionParser`, | ||
populate it with options, and parse the command line. | ||
:mod:`optparse` allows users to specify options in the conventional | ||
GNU/POSIX syntax, and additionally generates usage and help messages for you. | ||
|
||
Here's an example of using :mod:`optparse` in a simple script:: | ||
|
@@ -82,10 +192,11 @@ Background | |
---------- | ||
|
||
:mod:`optparse` was explicitly designed to encourage the creation of programs | ||
with straightforward, conventional command-line interfaces. To that end, it | ||
supports only the most common command-line syntax and semantics conventionally | ||
used under Unix. If you are unfamiliar with these conventions, read this | ||
section to acquaint yourself with them. | ||
with straightforward command-line interfaces that follow the conventions | ||
established by the :c:func:`!getopt` family of functions available to C developers. | ||
To that end, it supports only the most common command-line syntax and semantics | ||
conventionally used under Unix. If you are unfamiliar with these conventions, | ||
reading this section will allow you to acquaint yourself with them. | ||
|
||
|
||
.. _optparse-terminology: | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.