-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
gh-126180: Remove getopt and optparse deprecation notices #126227
base: main
Are you sure you want to change the base?
Changes from all commits
a560986
0a568f1
55b7215
0971780
09b0237
4cdebde
693a979
50d1ee4
4e50c74
0db2c88
e474e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,18 @@ | |
|
||
**Source code:** :source:`Lib/argparse.py` | ||
|
||
.. note:: | ||
|
||
While :mod:`argparse` is the recommended standard library module for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not recommend it for now. You can use it on your own risk. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this PR, I'd like to stick with the status quo as far as recommendations go (that is, only taking the step back from " Taking the extra step to saying " |
||
*implementing* basic command line applications, authors of third party | ||
command line argument processing libraries may find that the | ||
lower level :mod:`optparse` module serves as a better foundation for | ||
that use case. ``optparse`` (or one of the third party libraries | ||
based on it) may also be worth considering for applications where | ||
stricter adherence to common Unix and Linux command line interface | ||
conventions around the handling of option parameter values that start | ||
with ``-`` is desired. | ||
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not only this. There are other peculiarities that make implementation of compatible interface with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I had gotten the impression from the Discourse thread that this was the primary quirk, but I can reword these caveats to be less specific to that one issue. |
||
|
||
-------------- | ||
|
||
.. sidebar:: Tutorial | ||
|
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 | ||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would place |
||
getpass.rst | ||
fileinput.rst | ||
curses.rst | ||
curses.ascii.rst | ||
curses.panel.rst |
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,6 @@ | |
|
||
**Source code:** :source:`Lib/getopt.py` | ||
|
||
.. deprecated:: 3.13 | ||
The :mod:`getopt` module is :term:`soft deprecated` and will not be | ||
developed further; development will continue with the :mod:`argparse` | ||
module. | ||
|
||
.. note:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would convert the note in normal paragraph. Also, it should refer to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ended up moving this page back to the "Superseded" section of the docs, as it really is a niche use case when anyone should be reaching for this. The use of the note syntax here reflects that "You almost certainly don't want this module" status. I agree about referring readers to optparse in the note, though. |
||
|
||
The :mod:`getopt` module is a parser for command line options whose API is | ||
|
@@ -144,13 +139,25 @@ In a script, typical usage is something like this:: | |
output = a | ||
else: | ||
assert False, "unhandled option" | ||
# ... | ||
process(args, output=output, verbose=verbose) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
Note that an equivalent command line interface could be produced with less code | ||
and more informative help and error messages by using the :mod:`argparse` module:: | ||
and more informative help and error messages by using the :mod:`optparse` module:: | ||
|
||
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) | ||
|
||
An equivalent command line interface for this simple case can also be produced | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even in this simple example this is not an exact equivalent. For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we elaborate on the discrepancies here, it may help lay the foundation for weakening the current recommendation to use argparse in a future PR. I'll incorporate more of your notes here tomorrow. |
||
by using the :mod:`argparse` module:: | ||
|
||
import argparse | ||
|
||
|
@@ -162,8 +169,15 @@ and more informative help and error messages by using the :mod:`argparse` module | |
# ... do something with args.output ... | ||
# ... do something with args.verbose .. | ||
Comment on lines
169
to
170
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this to show the difference in the API between three modules parser.add_argument('rest', nargs='*')
args = parser.parse_args()
process(args.rest, output=args.output, verbose=args.verbose) |
||
|
||
In more complex cases (such as options which accept values), the behaviour | ||
of the ``argparse`` version may diverge from that of the ``getopt`` and | ||
``optparse`` versions due to the way ``argparse`` handles parameter | ||
values that start with ``-``. | ||
Comment on lines
+174
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference is not only in this. |
||
|
||
.. seealso:: | ||
|
||
Module :mod:`argparse` | ||
Alternative command line option and argument parsing library. | ||
Module :mod:`optparse` | ||
More object-oriented command line option parsing. | ||
|
||
Module :mod:`argparse` | ||
More opinionated command line option and argument parsing library. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,47 @@ | |
|
||
**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. | ||
.. note:: | ||
|
||
:mod:`argparse` (rather than this module) is the recommended standard | ||
library module for implementing command line applications unless one | ||
of the following caveats applies: | ||
|
||
* 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). | ||
|
||
These ``argparse`` caveats also mean that :mod:`optparse` is likely to | ||
provide a better foundation for library authors *writing* third party | ||
command line argument processing libraries. | ||
|
||
.. seealso:: | ||
|
||
The :pypi:`"click" package <click>` is an ``optparse`` based third party | ||
argument processing library which allows command line applications to be | ||
developed as a set of appropriately decorated command implementation | ||
functions. | ||
|
||
.. seealso:: | ||
|
||
The :pypi:`"Typer" package <click>` is a ``click`` based third party | ||
argument processing library which allows the use of annotated Python | ||
type hints to define an application's command line interface. | ||
Comment on lines
+33
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to duplicate these |
||
|
||
-------------- | ||
|
||
: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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replacing |
||
: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:: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
:mod:`getopt` and :mod:`optparse` are no longer marked as deprecated. | ||
There are legitimate reasons to use one of these modules in preference to | ||
:mod:`argparse`, and none of these modules are at risk of being removed | ||
from the standard library. Of the three, ``argparse`` remains the | ||
recommended default choice, *unless* one of the concerns noted at the top of | ||
the ``optparse`` module documentation applies. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may create an impression that
optparse
is lower level thangetopt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll reorder it to make it clear the "lower level" comparison is relative to
argparse