Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 507bdfc

Browse files
gh-126180: Undeprecate the optparse and getopt modules
1 parent fccf382 commit 507bdfc

File tree

8 files changed

+37
-38
lines changed

8 files changed

+37
-38
lines changed

Doc/library/allos.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ but they are available on most other systems as well. Here's an overview:
1515
os.rst
1616
io.rst
1717
time.rst
18+
optparse.rst
1819
argparse.rst
20+
getopt.rst
1921
logging.rst
2022
logging.config.rst
2123
logging.handlers.rst

Doc/library/argparse.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ will figure out how to parse those out of :data:`sys.argv`. The :mod:`!argparse
2525
module also automatically generates help and usage messages. The module
2626
will also issue errors when users give the program invalid arguments.
2727

28+
.. note::
29+
Fine details of the command-line interface built with using the
30+
:mod:`!argparse` module differ from common Unix and Linux programs.
31+
If you want to implement more compatible interface, consider using
32+
the :mod:`optparse` or :mod:`getopt` modules.
33+
2834
The :mod:`!argparse` module's support for command-line interfaces is built
2935
around an instance of :class:`argparse.ArgumentParser`. It is a container for
3036
argument specifications and has options that apply to the parser as whole::

Doc/library/getopt.rst

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@
77

88
**Source code:** :source:`Lib/getopt.py`
99

10-
.. deprecated:: 3.13
11-
The :mod:`getopt` module is :term:`soft deprecated` and will not be
12-
developed further; development will continue with the :mod:`argparse`
13-
module.
14-
15-
.. note::
16-
17-
The :mod:`getopt` module is a parser for command line options whose API is
18-
designed to be familiar to users of the C :c:func:`!getopt` function. Users who
19-
are unfamiliar with the C :c:func:`!getopt` function or who would like to write
20-
less code and get better help and error messages should consider using the
21-
:mod:`argparse` module instead.
22-
23-
--------------
24-
2510
This module helps scripts to parse the command line arguments in ``sys.argv``.
2611
It supports the same conventions as the Unix :c:func:`!getopt` function (including
2712
the special meanings of arguments of the form '``-``' and '``--``'). Long
2813
options similar to those supported by GNU software may be used as well via an
2914
optional third argument.
3015

16+
The :mod:`getopt` module is a parser for command line options whose API is
17+
designed to be familiar to users of the C :c:func:`!getopt` function. Users who
18+
are unfamiliar with the C :c:func:`!getopt` function or who would like to write
19+
less code and get better help and error messages should consider using the
20+
:mod:`optparse` or :mod:`argparse` module instead.
21+
3122
This module provides two functions and an
3223
exception:
3324

@@ -144,26 +135,41 @@ In a script, typical usage is something like this::
144135
output = a
145136
else:
146137
assert False, "unhandled option"
147-
# ...
138+
process(args, output=output, verbose=verbose)
148139

149140
if __name__ == "__main__":
150141
main()
151142

152143
Note that an equivalent command line interface could be produced with less code
153-
and more informative help and error messages by using the :mod:`argparse` module::
144+
and more informative help and error messages by using the :mod:`optparse` module::
145+
146+
import optparse
147+
148+
if __name__ == '__main__':
149+
parser = optparse.OptionParser()
150+
parser.add_option('-o', '--output')
151+
parser.add_option('-v', dest='verbose', action='store_true')
152+
opts, args = parser.parse_args()
153+
process(args, output=opts.output, verbose=opts.verbose)
154+
155+
A roughtly equivalent command line interface could also be produced by using
156+
the :mod:`argparse` module::
154157

155158
import argparse
156159

157160
if __name__ == '__main__':
158161
parser = argparse.ArgumentParser()
159162
parser.add_argument('-o', '--output')
160163
parser.add_argument('-v', dest='verbose', action='store_true')
164+
parser.add_argument('rest', nargs='*')
161165
args = parser.parse_args()
162-
# ... do something with args.output ...
163-
# ... do something with args.verbose ..
166+
process(args.rest, output=args.output, verbose=args.verbose)
164167

165168
.. seealso::
166169

170+
Module :mod:`optparse`
171+
More object-oriented command line option parsing.
172+
167173
Module :mod:`argparse`
168174
Alternative command line option and argument parsing library.
169175

Doc/library/optparse.rst

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@
33

44
.. module:: optparse
55
:synopsis: Command-line option parsing library.
6-
:deprecated:
76

87
.. moduleauthor:: Greg Ward <[email protected]>
98
.. sectionauthor:: Greg Ward <[email protected]>
109

1110
**Source code:** :source:`Lib/optparse.py`
1211

13-
.. deprecated:: 3.2
14-
The :mod:`optparse` module is :term:`soft deprecated` and will not be
15-
developed further; development will continue with the :mod:`argparse`
16-
module.
17-
18-
--------------
19-
2012
:mod:`optparse` is a more convenient, flexible, and powerful library for parsing
21-
command-line options than the old :mod:`getopt` module. :mod:`optparse` uses a
13+
command-line options than the simple :mod:`getopt` module. :mod:`optparse` uses a
2214
more declarative style of command-line parsing: you create an instance of
2315
:class:`OptionParser`, populate it with options, and parse the command
2416
line. :mod:`optparse` allows users to specify options in the conventional

Doc/library/superseded.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ backwards compatibility. They have been superseded by other modules.
1111
.. toctree::
1212
:maxdepth: 1
1313

14-
getopt.rst
15-
optparse.rst

Doc/whatsnew/3.13.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,14 +1787,6 @@ New Deprecations
17871787
Check membership in :data:`~dis.hasarg` instead.
17881788
(Contributed by Irit Katriel in :gh:`109319`.)
17891789

1790-
* :mod:`getopt` and :mod:`optparse`:
1791-
1792-
* Both modules are now :term:`soft deprecated`,
1793-
with :mod:`argparse` preferred for new projects.
1794-
This is a new soft-deprecation for the :mod:`!getopt` module,
1795-
whereas the :mod:`!optparse` module was already *de facto* soft deprecated.
1796-
(Contributed by Victor Stinner in :gh:`106535`.)
1797-
17981790
* :mod:`gettext`:
17991791

18001792
* Deprecate non-integer numbers as arguments to functions and methods

Doc/whatsnew/3.14.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ asyncio
463463
Deprecated
464464
==========
465465

466+
The :mod:`optparse` and :mod:`getopt` modules are no longer considered deprecated.
467+
466468
* :mod:`argparse`:
467469
Passing the undocumented keyword argument *prefix_chars* to
468470
:meth:`~argparse.ArgumentParser.add_argument_group` is now
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The :mod:`optparse` and :mod:`getopt` modules are no longer deprecated.

0 commit comments

Comments
 (0)