From 097cba2f6cb4376ad68df82503eda04beb016cfc Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 30 Oct 2024 16:57:39 +0200 Subject: [PATCH 1/5] gh-126180: Undeprecate the optparse and getopt modules --- Doc/library/allos.rst | 2 + Doc/library/argparse.rst | 6 +++ Doc/library/getopt.rst | 44 +++++++++++-------- Doc/library/optparse.rst | 10 +---- Doc/library/superseded.rst | 2 - Doc/whatsnew/3.13.rst | 8 ---- Doc/whatsnew/3.14.rst | 2 + ...-10-30-16-56-54.gh-issue-126180.CQ-bA4.rst | 1 + 8 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-10-30-16-56-54.gh-issue-126180.CQ-bA4.rst diff --git a/Doc/library/allos.rst b/Doc/library/allos.rst index 0223c1054ea5d8..a7d88025ac546f 100644 --- a/Doc/library/allos.rst +++ b/Doc/library/allos.rst @@ -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 diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 940698218534e0..9a507701bf3331 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -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:: + Fine details of the command-line interface built with using the + :mod:`!argparse` module differ from common Unix and Linux programs. + If you want to implement 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:: diff --git a/Doc/library/getopt.rst b/Doc/library/getopt.rst index d43d3250732306..42ab153bb2cc8b 100644 --- a/Doc/library/getopt.rst +++ b/Doc/library/getopt.rst @@ -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` module instead. + This module provides two functions and an exception: @@ -144,13 +135,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) + +A roughtly equivalent command line interface could also be produced by using +the :mod:`argparse` module:: import argparse @@ -158,12 +161,15 @@ and more informative help and error messages by using the :mod:`argparse` module 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. diff --git a/Doc/library/optparse.rst b/Doc/library/optparse.rst index 74a49a8fb33666..e0f9a4af5ec084 100644 --- a/Doc/library/optparse.rst +++ b/Doc/library/optparse.rst @@ -3,22 +3,14 @@ .. module:: optparse :synopsis: Command-line option parsing library. - :deprecated: .. moduleauthor:: Greg Ward .. sectionauthor:: Greg Ward **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 diff --git a/Doc/library/superseded.rst b/Doc/library/superseded.rst index 17bfa66f043302..046207011daee1 100644 --- a/Doc/library/superseded.rst +++ b/Doc/library/superseded.rst @@ -11,5 +11,3 @@ backwards compatibility. They have been superseded by other modules. .. toctree:: :maxdepth: 1 - getopt.rst - optparse.rst diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index de4c7fd4c0486b..4e045a79f46120 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -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`: - - * 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 diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 7f9e3107a6e1a0..0ebf72130d7b0a 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -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 diff --git a/Misc/NEWS.d/next/Library/2024-10-30-16-56-54.gh-issue-126180.CQ-bA4.rst b/Misc/NEWS.d/next/Library/2024-10-30-16-56-54.gh-issue-126180.CQ-bA4.rst new file mode 100644 index 00000000000000..575e3bdbe5c2a3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-30-16-56-54.gh-issue-126180.CQ-bA4.rst @@ -0,0 +1 @@ +The :mod:`optparse` and :mod:`getopt` modules are no longer deprecated. From 9744ded874a41b328ef8c37332d1dbfa0321b23e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 30 Oct 2024 17:48:07 +0200 Subject: [PATCH 2/5] Update Doc/library/getopt.rst Co-authored-by: Carol Willing --- Doc/library/getopt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/getopt.rst b/Doc/library/getopt.rst index 42ab153bb2cc8b..6d41f3be3f14ca 100644 --- a/Doc/library/getopt.rst +++ b/Doc/library/getopt.rst @@ -152,7 +152,7 @@ and more informative help and error messages by using the :mod:`optparse` module opts, args = parser.parse_args() process(args, output=opts.output, verbose=opts.verbose) -A roughtly equivalent command line interface could also be produced by using +A roughly equivalent command line interface could also be produced by using the :mod:`argparse` module:: import argparse From 8f211ff0a9744f0b636be16b20fc86b945c18e8b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 31 Oct 2024 07:34:24 +0200 Subject: [PATCH 3/5] Update Doc/library/getopt.rst Co-authored-by: Martin Panter --- Doc/library/getopt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/getopt.rst b/Doc/library/getopt.rst index 6d41f3be3f14ca..247ed830d56aa6 100644 --- a/Doc/library/getopt.rst +++ b/Doc/library/getopt.rst @@ -17,7 +17,7 @@ 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` module instead. +:mod:`optparse` or :mod:`argparse` modules instead. This module provides two functions and an exception: From 22b8a773c2c6f70f195acc021124acb574c969ab Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 31 Oct 2024 07:35:14 +0200 Subject: [PATCH 4/5] Update Doc/library/argparse.rst Co-authored-by: Martin Panter --- Doc/library/argparse.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 9a507701bf3331..bbd9267c2a7f08 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -28,7 +28,7 @@ will also issue errors when users give the program invalid arguments. .. note:: Fine details of the command-line interface built with using the :mod:`!argparse` module differ from common Unix and Linux programs. - If you want to implement more compatible interface, consider using + 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 From 2c2f48f39e4e6a2f00825209792e4215be098c44 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 31 Oct 2024 07:35:25 +0200 Subject: [PATCH 5/5] Update Doc/library/argparse.rst Co-authored-by: Martin Panter --- Doc/library/argparse.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index bbd9267c2a7f08..fc665403dcca42 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -26,7 +26,7 @@ module also automatically generates help and usage messages. The module will also issue errors when users give the program invalid arguments. .. note:: - Fine details of the command-line interface built with using the + 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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy