Skip to content

Commit 9845e44

Browse files
Move details about upgrading from optparse to a separate doc
1 parent 47a411a commit 9845e44

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

Doc/library/argparse-optparse.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.. currentmodule:: argparse
2+
3+
.. _upgrading-optparse-code:
4+
5+
==========================
6+
Upgrading optparse code
7+
==========================
8+
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 the new
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 standard library :mod:`optparse`
17+
module in a number of ways including:
18+
19+
* Handling positional arguments.
20+
* Supporting sub-commands.
21+
* Allowing alternative option prefixes like ``+`` and ``/``.
22+
* Handling zero-or-more and one-or-more style arguments.
23+
* Producing more informative usage messages.
24+
* Providing a much simpler interface for custom ``type`` and ``action``.
25+
26+
A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
27+
28+
* Replace all :meth:`optparse.OptionParser.add_option` calls with
29+
:meth:`ArgumentParser.add_argument` calls.
30+
31+
* Replace ``(options, args) = parser.parse_args()`` with ``args =
32+
parser.parse_args()`` and add additional :meth:`ArgumentParser.add_argument`
33+
calls for the positional arguments. Keep in mind that what was previously
34+
called ``options``, now in the :mod:`argparse` context is called ``args``.
35+
36+
* Replace :meth:`optparse.OptionParser.disable_interspersed_args`
37+
by using :meth:`~ArgumentParser.parse_intermixed_args` instead of
38+
:meth:`~ArgumentParser.parse_args`.
39+
40+
* Replace callback actions and the ``callback_*`` keyword arguments with
41+
``type`` or ``action`` arguments.
42+
43+
* Replace string names for ``type`` keyword arguments with the corresponding
44+
type objects (e.g. int, float, complex, etc).
45+
46+
* Replace :class:`optparse.Values` with :class:`Namespace` and
47+
:exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with
48+
:exc:`ArgumentError`.
49+
50+
* Replace strings with implicit arguments such as ``%default`` or ``%prog`` with
51+
the standard Python syntax to use dictionaries to format strings, that is,
52+
``%(default)s`` and ``%(prog)s``.
53+
54+
* Replace the OptionParser constructor ``version`` argument with a call to
55+
``parser.add_argument('--version', action='version', version='<the version>')``.

Doc/library/argparse.rst

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,56 +2053,6 @@ remaining unparsed argument strings.
20532053

20542054
.. _upgrading-optparse-code:
20552055

2056-
Upgrading optparse code
2057-
-----------------------
2058-
2059-
Originally, the :mod:`argparse` module had attempted to maintain compatibility
2060-
with :mod:`optparse`. However, :mod:`optparse` was difficult to extend
2061-
transparently, particularly with the changes required to support the new
2062-
``nargs=`` specifiers and better usage messages. When most everything in
2063-
:mod:`optparse` had either been copy-pasted over or monkey-patched, it no
2064-
longer seemed practical to try to maintain the backwards compatibility.
2065-
2066-
The :mod:`argparse` module improves on the standard library :mod:`optparse`
2067-
module in a number of ways including:
2068-
2069-
* Handling positional arguments.
2070-
* Supporting sub-commands.
2071-
* Allowing alternative option prefixes like ``+`` and ``/``.
2072-
* Handling zero-or-more and one-or-more style arguments.
2073-
* Producing more informative usage messages.
2074-
* Providing a much simpler interface for custom ``type`` and ``action``.
2075-
2076-
A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
2077-
2078-
* Replace all :meth:`optparse.OptionParser.add_option` calls with
2079-
:meth:`ArgumentParser.add_argument` calls.
2080-
2081-
* Replace ``(options, args) = parser.parse_args()`` with ``args =
2082-
parser.parse_args()`` and add additional :meth:`ArgumentParser.add_argument`
2083-
calls for the positional arguments. Keep in mind that what was previously
2084-
called ``options``, now in the :mod:`argparse` context is called ``args``.
2085-
2086-
* Replace :meth:`optparse.OptionParser.disable_interspersed_args`
2087-
by using :meth:`~ArgumentParser.parse_intermixed_args` instead of
2088-
:meth:`~ArgumentParser.parse_args`.
2089-
2090-
* Replace callback actions and the ``callback_*`` keyword arguments with
2091-
``type`` or ``action`` arguments.
2092-
2093-
* Replace string names for ``type`` keyword arguments with the corresponding
2094-
type objects (e.g. int, float, complex, etc).
2095-
2096-
* Replace :class:`optparse.Values` with :class:`Namespace` and
2097-
:exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with
2098-
:exc:`ArgumentError`.
2099-
2100-
* Replace strings with implicit arguments such as ``%default`` or ``%prog`` with
2101-
the standard Python syntax to use dictionaries to format strings, that is,
2102-
``%(default)s`` and ``%(prog)s``.
2103-
2104-
* Replace the OptionParser constructor ``version`` argument with a call to
2105-
``parser.add_argument('--version', action='version', version='<the version>')``.
21062056

21072057
Exceptions
21082058
----------

0 commit comments

Comments
 (0)