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

Skip to content

gh-126180: Undeprecate getopt and optparse #126186

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/library/allos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ but they are available on most other systems as well. Here's an overview:
os.rst
io.rst
time.rst
optparse.rst
argparse.rst
getopt.rst
logging.rst
logging.config.rst
logging.handlers.rst
Expand Down
6 changes: 6 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ will figure out how to parse those out of :data:`sys.argv`. The :mod:`!argparse
module also automatically generates help and usage messages. The module
will also issue errors when users give the program invalid arguments.

.. note::
The finer details of the command-line interface built with the
:mod:`!argparse` module differ from common Unix and Linux programs.
If you want to implement a more compatible interface, consider using
the :mod:`optparse` or :mod:`getopt` modules.

The :mod:`!argparse` module's support for command-line interfaces is built
around an instance of :class:`argparse.ArgumentParser`. It is a container for
argument specifications and has options that apply to the parser as whole::
Expand Down
44 changes: 25 additions & 19 deletions Doc/library/getopt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@

**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::

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 helps scripts to parse the command line arguments in ``sys.argv``.
It supports the same conventions as the Unix :c:func:`!getopt` function (including
the special meanings of arguments of the form '``-``' and '``--``'). Long
options similar to those supported by GNU software may be used as well via an
optional third argument.

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:`optparse` or :mod:`argparse` modules instead.

This module provides two functions and an
exception:

Expand Down Expand Up @@ -144,26 +135,41 @@ 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)

A roughly equivalent command line interface could 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')
parser.add_argument('rest', nargs='*')
args = parser.parse_args()
# ... do something with args.output ...
# ... do something with args.verbose ..
process(args.rest, output=args.output, verbose=args.verbose)

.. seealso::

Module :mod:`optparse`
More object-oriented command line option parsing.

Module :mod:`argparse`
Alternative command line option and argument parsing library.

10 changes: 1 addition & 9 deletions Doc/library/optparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@

.. 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.

--------------

: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
command-line options than the simple :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
Expand Down
2 changes: 0 additions & 2 deletions Doc/library/superseded.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ backwards compatibility. They have been superseded by other modules.
.. toctree::
:maxdepth: 1

getopt.rst
optparse.rst
8 changes: 0 additions & 8 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1787,14 +1787,6 @@ New Deprecations
Check membership in :data:`~dis.hasarg` instead.
(Contributed by Irit Katriel in :gh:`109319`.)

* :mod:`getopt` and :mod:`optparse`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we typically remove from the What's New for the version introduced or do we annotate that it is being changed in 3.14?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting question. Obviously undeprecation should be retroactive. There is no sense in discouraging the use of getopt and optparse in 3.12 and 3.13. For 3.13, this may even be considered a bugfix.

But how to indicate this in What's New? If say nothing, then users will continue to think that getopt and optparse are deprecated. If write about this in 3.12 and 3.13, most users will not read this. So we should write something in 3.14, even if this change will be applied to 3.12 and 3.13.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a good answer to this. Perhaps @hugovk would like to weigh in as release manager.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yhg1s is the 3.13 RM so punting to him :)

However, optparse was deprecated back in 3.2, some 13 years ago, so for one of them, this isn't a sudden change in 3.13.

If we do undeprecate, perhaps adding a note to the existing "What's New in 3.13" entry to note it's future change?

In any case, I would also hold off merging this and allow some more time for the discussion, it's not yet been 24 hours:

https://discuss.python.org/t/getopt-and-optparse-vs-argparse/69618

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In drafting my PR, I took the view that 3.13 is new enough that most of the people who will read the What's New haven't actually read it yet, and hence just changed this to a regular 3.13 What's New entry for optparse (getopt is reverting to the 3.12 status quo, so it only got mentioned in NEWS).


* Both modules are now :term:`soft deprecated`,
with :mod:`argparse` preferred for new projects.
This is a new soft-deprecation for the :mod:`!getopt` module,
whereas the :mod:`!optparse` module was already *de facto* soft deprecated.
(Contributed by Victor Stinner in :gh:`106535`.)

* :mod:`gettext`:

* Deprecate non-integer numbers as arguments to functions and methods
Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ asyncio
Deprecated
==========

The :mod:`optparse` and :mod:`getopt` modules are no longer considered deprecated.

* :mod:`argparse`:

* Passing the undocumented keyword argument *prefix_chars* to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :mod:`optparse` and :mod:`getopt` modules are no longer deprecated.
Loading