Skip to content

Commit ae16b99

Browse files
bitdancerMariatta
authored andcommitted
[2.7] Clarify nature of parse_args 'args' argument. (GH-3292) (GH-3328)
Patch by Paul.j3. Includes an unrelated but useful addition to the optparse porting section. (cherry picked from commit 0c7983e)
1 parent 1911cf3 commit ae16b99

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Doc/library/argparse.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ values are:
854854
usage: PROG [-h] foo [foo ...]
855855
PROG: error: too few arguments
856856

857+
.. _`argparse.REMAINDER`:
858+
857859
* ``argparse.REMAINDER``. All the remaining command-line arguments are gathered
858860
into a list. This is commonly useful for command line utilities that dispatch
859861
to other command line utilities::
@@ -1275,8 +1277,11 @@ The parse_args() method
12751277
created and how they are assigned. See the documentation for
12761278
:meth:`add_argument` for details.
12771279

1278-
By default, the argument strings are taken from :data:`sys.argv`, and a new empty
1279-
:class:`Namespace` object is created for the attributes.
1280+
* args_ - List of strings to parse. The default is taken from
1281+
:data:`sys.argv`.
1282+
1283+
* namespace_ - An object to take the attributes. The default is a new empty
1284+
:class:`Namespace` object.
12801285

12811286

12821287
Option value syntax
@@ -1417,6 +1422,7 @@ a unique option)::
14171422

14181423
An error is produced for arguments that could produce more than one options.
14191424

1425+
.. _args:
14201426

14211427
Beyond ``sys.argv``
14221428
^^^^^^^^^^^^^^^^^^^
@@ -1438,6 +1444,7 @@ interactive prompt::
14381444
>>> parser.parse_args(['1', '2', '3', '4', '--sum'])
14391445
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
14401446

1447+
.. _namespace:
14411448

14421449
The Namespace object
14431450
^^^^^^^^^^^^^^^^^^^^
@@ -1943,7 +1950,12 @@ A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
19431950
* Replace ``(options, args) = parser.parse_args()`` with ``args =
19441951
parser.parse_args()`` and add additional :meth:`ArgumentParser.add_argument`
19451952
calls for the positional arguments. Keep in mind that what was previously
1946-
called ``options``, now in :mod:`argparse` context is called ``args``.
1953+
called ``options``, now in the :mod:`argparse` context is called ``args``.
1954+
1955+
* Replace :meth:`optparse.OptionParser.disable_interspersed_args`
1956+
by setting ``nargs`` of a positional argument to `argparse.REMAINDER`_, or
1957+
use :meth:`~ArgumentParser.parse_known_args` to collect unparsed argument
1958+
strings in a separate list.
19471959

19481960
* Replace callback actions and the ``callback_*`` keyword arguments with
19491961
``type`` or ``action`` arguments.

0 commit comments

Comments
 (0)