Skip to content

Commit

Permalink
Merge branch 'main' into small_int_immortal_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
eendebakpt authored Dec 23, 2024
2 parents cfb9120 + 180d417 commit b606c09
Show file tree
Hide file tree
Showing 20 changed files with 353 additions and 76 deletions.
1 change: 1 addition & 0 deletions .github/workflows/reusable-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
brew link --overwrite tcl-tk@8
- name: Configure CPython
run: |
MACOSX_DEPLOYMENT_TARGET=10.15 \
GDBM_CFLAGS="-I$(brew --prefix gdbm)/include" \
GDBM_LIBS="-L$(brew --prefix gdbm)/lib -lgdbm" \
./configure \
Expand Down
36 changes: 23 additions & 13 deletions Doc/howto/argparse-optparse.rst
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
.. currentmodule:: argparse

.. _upgrading-optparse-code:
.. _migrating-optparse-code:

==========================
Upgrading optparse code
==========================
============================================
Migrating ``optparse`` code to ``argparse``
============================================

Originally, the :mod:`argparse` module had attempted to maintain compatibility
with :mod:`optparse`. However, :mod:`optparse` was difficult to extend
transparently, particularly with the changes required to support
``nargs=`` specifiers and better usage messages. When most everything in
:mod:`optparse` had either been copy-pasted over or monkey-patched, it no
longer seemed practical to try to maintain the backwards compatibility.

The :mod:`argparse` module improves on the :mod:`optparse`
module in a number of ways including:
The :mod:`argparse` module offers several higher level features not natively
provided by the :mod:`optparse` module, including:

* Handling positional arguments.
* Supporting subcommands.
Expand All @@ -23,7 +17,23 @@ module in a number of ways including:
* Producing more informative usage messages.
* Providing a much simpler interface for custom ``type`` and ``action``.

A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
Originally, the :mod:`argparse` module attempted to maintain compatibility
with :mod:`optparse`. However, the fundamental design differences between
supporting declarative command line option processing (while leaving positional
argument processing to application code), and supporting both named options
and positional arguments in the declarative interface mean that the
API has diverged from that of ``optparse`` over time.

As described in :ref:`choosing-an-argument-parser`, applications that are
currently using :mod:`optparse` and are happy with the way it works can
just continue to use ``optparse``.

Application developers that are considering migrating should also review
the list of intrinsic behavioural differences described in that section
before deciding whether or not migration is desirable.

For applications that do choose to migrate from :mod:`optparse` to :mod:`argparse`,
the following suggestions should be helpful:

* Replace all :meth:`optparse.OptionParser.add_option` calls with
:meth:`ArgumentParser.add_argument` calls.
Expand Down
15 changes: 10 additions & 5 deletions Doc/howto/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ recommended command-line parsing module in the Python standard library.

.. note::

There are two other modules that fulfill the same task, namely
:mod:`getopt` (an equivalent for ``getopt()`` from the C
language) and the deprecated :mod:`optparse`.
Note also that :mod:`argparse` is based on :mod:`optparse`,
and therefore very similar in terms of usage.
The standard library includes two other libraries directly related
to command-line parameter processing: the lower level :mod:`optparse`
module (which may require more code to configure for a given application,
but also allows an application to request behaviors that ``argparse``
doesn't support), and the very low level :mod:`getopt` (which specifically
serves as an equivalent to the :c:func:`!getopt` family of functions
available to C programmers).
While neither of those modules is covered directly in this guide, many of
the core concepts in ``argparse`` first originated in ``optparse``, so
some aspects of this tutorial will also be relevant to ``optparse`` users.


Concepts
Expand Down
5 changes: 0 additions & 5 deletions Doc/library/allos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ but they are available on most other systems as well. Here's an overview:
os.rst
io.rst
time.rst
argparse.rst
logging.rst
logging.config.rst
logging.handlers.rst
getpass.rst
curses.rst
curses.ascii.rst
curses.panel.rst
platform.rst
errno.rst
ctypes.rst
12 changes: 12 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@

**Source code:** :source:`Lib/argparse.py`

.. note::

While :mod:`argparse` is the default recommended standard library module
for implementing basic command line applications, authors with more
exacting requirements for exactly how their command line applications
behave may find it doesn't provide the necessary level of control.
Refer to :ref:`choosing-an-argument-parser` for alternatives to
consider when ``argparse`` doesn't support behaviors that the application
requires (such as entirely disabling support for interspersed options and
positional arguments, or accepting option parameter values that start
with ``-`` even when they correspond to another defined option).

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

.. sidebar:: Tutorial
Expand Down
21 changes: 21 additions & 0 deletions Doc/library/cmdlinelibs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.. _cmdlinelibs:

********************************
Command Line Interface Libraries
********************************

The modules described in this chapter assist with implementing
command line and terminal interfaces for applications.

Here's an overview:

.. toctree::
:maxdepth: 1

argparse.rst
optparse.rst
getpass.rst
fileinput.rst
curses.rst
curses.ascii.rst
curses.panel.rst
1 change: 0 additions & 1 deletion Doc/library/filesys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ in this chapter is:

pathlib.rst
os.path.rst
fileinput.rst
stat.rst
filecmp.rst
tempfile.rst
Expand Down
54 changes: 38 additions & 16 deletions Doc/library/getopt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@

**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 is considered feature complete. A more declarative 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.

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

Expand All @@ -28,6 +23,13 @@ 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.

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. See :ref:`choosing-an-argument-parser` for
additional details.

This module provides two functions and an
exception:

Expand Down Expand Up @@ -194,13 +196,27 @@ 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:

.. testcode::

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 for this case can also be
produced by using the :mod:`argparse` module:

.. testcode::

Expand All @@ -210,12 +226,18 @@ 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)

See :ref:`choosing-an-argument-parser` for details on how the ``argparse``
version of this code differs in behaviour from the ``optparse`` (and
``getopt``) version.

.. seealso::

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

Module :mod:`argparse`
More opinionated command line option and argument parsing library.
1 change: 1 addition & 0 deletions Doc/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ the `Python Package Index <https://pypi.org>`_.
fileformats.rst
crypto.rst
allos.rst
cmdlinelibs.rst
concurrency.rst
ipc.rst
netdata.rst
Expand Down
Loading

0 comments on commit b606c09

Please sign in to comment.