GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-126180: Undeprecate getopt and optparse by serhiy-storchaka · Pull Request #126186 · python/cpython

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.

Sign up for GitHub

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
serhiy-storchaka wants to merge 5 commits into python:main from serhiy-storchaka:undeprecate-getopt-optparse

Uh oh!

There was an error while loading. Please reload this page.

Closed

gh-126180: Undeprecate getopt and optparse #126186

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
097cba2
gh-126180: Undeprecate the optparse and getopt modules
serhiy-storchaka Oct 30, 2024
9744ded
Update Doc/library/getopt.rst
serhiy-storchaka Oct 30, 2024
8f211ff
Update Doc/library/getopt.rst
serhiy-storchaka Oct 31, 2024
22b8a77
Update Doc/library/argparse.rst
serhiy-storchaka Oct 31, 2024
2c2f48f
Update Doc/library/argparse.rst
serhiy-storchaka Oct 31, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 Doc/library/allos.rst
Show comments
View file
Open in desktop
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 6 Doc/library/argparse.rst
Show comments
View file
Open in desktop
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 44 Doc/library/getopt.rst
Show comments
View file
Open in desktop
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 10 Doc/library/optparse.rst
Show comments
View file
Open in desktop
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 2 Doc/library/superseded.rst
Show comments
View file
Open in desktop
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 8 Doc/whatsnew/3.13.rst
Show comments
View file
Open in desktop
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`:
Comment thread

@willingc willingc Oct 30, 2024

Copy link
Copy Markdown
Contributor

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.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Oct 30, 2024

Copy link
Copy Markdown
Member Author

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

willingc reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@willingc willingc Oct 30, 2024

Copy link
Copy Markdown
Contributor

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 don't have a good answer to this. Perhaps @hugovk would like to weigh in as release manager.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@hugovk hugovk Oct 30, 2024

Copy link
Copy Markdown
Member

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.

@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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka and willingc reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

@ncoghlan ncoghlan Oct 31, 2024

Copy link
Copy Markdown
Contributor

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

* 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 2 Doc/whatsnew/3.14.rst
Show comments
View file
Open in desktop
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
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2024-10-30-16-56-54.gh-issue-126180.CQ-bA4.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :mod:`optparse` and :mod:`getopt` modules are no longer deprecated.
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.