-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Changes from 1 commit
a560986
0a568f1
55b7215
0971780
09b0237
4cdebde
693a979
50d1ee4
4e50c74
0db2c88
e474e77
43234de
93d7161
bb91b52
7cc72ad
7f51456
5d86774
c2ae293
5e2fcd9
457ee88
f67d20c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ | |
|
||
.. note:: | ||
gpshead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The :mod:`getopt` module is a parser for command line options whose API is | ||
designed to be familiar to users of the C :c:func:`!getopt` function. Users who | ||
are unfamiliar with the C :c:func:`!getopt` function or who would like to write | ||
less code and get better help and error messages should consider using the | ||
:mod:`argparse` module instead. | ||
This module is considered feature complete. A more object-oriented and | ||
extensible alternative to this API is provided in the :mod:`optparse` | ||
module. Further functional enhancements for command line parameter | ||
processing are provided either as third party modules on PyPI, | ||
or else as features in the :mod:`argparse` module. | ||
|
||
-------------- | ||
|
||
|
@@ -23,6 +23,12 @@ | |
options similar to those supported by GNU software may be used as well via an | ||
optional third argument. | ||
|
||
Users who are unfamiliar with the Unix :c:func:`!getopt` function should consider | ||
using the :mod:`argparse` module instead. Users who are familiar with the Unix | ||
:c:func:`!getopt` function, but would like to get equivalent behavior while | ||
writing less code and getting better help and error messages should consider | ||
using the :mod:`optparse` module. | ||
|
||
This module provides two functions and an | ||
exception: | ||
|
||
|
@@ -150,29 +156,49 @@ | |
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 | ||
by using the :mod:`argparse` module:: | ||
|
||
import argparse | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-o', '--output') | ||
parser.add_argument('-v', dest='verbose', action='store_true') | ||
args = parser.parse_args() | ||
# ... do something with args.output ... | ||
# ... do something with 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 ``-``. | ||
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) | ||
|
||
A roughly equivalent command line interface for this case can also be | ||
produced by using the :mod:`argparse` module:: | ||
|
||
import argparse | ||
ncoghlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
|
||
However, unlike the ``optparse`` example, this ``argparse`` example will | ||
handle some parameter combinations differently from the way the ``getopt`` | ||
version would handle them. For example (amongst other differences): | ||
|
||
* supplying ``-o -v`` gives ``output="-v"`` and ``verbose=False`` | ||
for both ``getopt`` and ``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=()`` | ||
for both ``getopt`` and ``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"`` | ||
for both ``getopt`` and ``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. | ||
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. @serhiy-storchaka ^^^ is the main reason I don't think we should walk back the default argparse recommendation. Many (probably even most) Python devs would consider argparse to be in the right for these 3 examples, and getopt/optparse the ones doing something dubious (even if what they're doing is the common Unix convention). The discrepancies are valid reasons for reverting the soft deprecation (optparse can genuinely be used to define command line behaviours that argparse doesn't support), but it will take less arguable examples than these ones to make the case that optparse should be preferred over argparse in general. 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 would be better to write about these peculiarities in the 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. agreed. I like that we're giving a list of differences in behavior, but I think it makes more sense to put this in a common section :ref:'d from all three of our command line parsing module docs. I don't expect anyone reading argparse (most likely) or optparse (likely) docs to find this in the getopt docs (presumed least likely to be read). 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've moved this to a new "Choosing an argument parsing library" section at the start of the Similar, I made the note at the start of the |
||
|
||
.. seealso:: | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.