Skip to content

Commit 487d428

Browse files
committedDec 23, 2024··
Deploying to gh-pages from @ e7b5675 🚀
1 parent 70254cc commit 487d428

File tree

563 files changed

+2556
-2502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

563 files changed

+2556
-2502
lines changed
 

‎_sources/howto/argparse-optparse.rst.txt

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
.. currentmodule:: argparse
22

33
.. _upgrading-optparse-code:
4+
.. _migrating-optparse-code:
45

5-
==========================
6-
Upgrading optparse code
7-
==========================
6+
============================================
7+
Migrating ``optparse`` code to ``argparse``
8+
============================================
89

9-
Originally, the :mod:`argparse` module had attempted to maintain compatibility
10-
with :mod:`optparse`. However, :mod:`optparse` was difficult to extend
11-
transparently, particularly with the changes required to support
12-
``nargs=`` specifiers and better usage messages. When most everything in
13-
:mod:`optparse` had either been copy-pasted over or monkey-patched, it no
14-
longer seemed practical to try to maintain the backwards compatibility.
15-
16-
The :mod:`argparse` module improves on the :mod:`optparse`
17-
module in a number of ways including:
10+
The :mod:`argparse` module offers several higher level features not natively
11+
provided by the :mod:`optparse` module, including:
1812

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

26-
A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
20+
Originally, the :mod:`argparse` module attempted to maintain compatibility
21+
with :mod:`optparse`. However, the fundamental design differences between
22+
supporting declarative command line option processing (while leaving positional
23+
argument processing to application code), and supporting both named options
24+
and positional arguments in the declarative interface mean that the
25+
API has diverged from that of ``optparse`` over time.
26+
27+
As described in :ref:`choosing-an-argument-parser`, applications that are
28+
currently using :mod:`optparse` and are happy with the way it works can
29+
just continue to use ``optparse``.
30+
31+
Application developers that are considering migrating should also review
32+
the list of intrinsic behavioural differences described in that section
33+
before deciding whether or not migration is desirable.
34+
35+
For applications that do choose to migrate from :mod:`optparse` to :mod:`argparse`,
36+
the following suggestions should be helpful:
2737

2838
* Replace all :meth:`optparse.OptionParser.add_option` calls with
2939
:meth:`ArgumentParser.add_argument` calls.

‎_sources/howto/argparse.rst.txt

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ recommended command-line parsing module in the Python standard library.
1313

1414
.. note::
1515

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

2227

2328
Concepts

0 commit comments

Comments
 (0)