Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f869322

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

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

Doc/library/argparse.rst

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

903+
.. _`argparse.REMAINDER`:
904+
903905
* ``argparse.REMAINDER``. All the remaining command-line arguments are gathered
904906
into a list. This is commonly useful for command line utilities that dispatch
905907
to other command line utilities::
@@ -1326,8 +1328,11 @@ The parse_args() method
13261328
created and how they are assigned. See the documentation for
13271329
:meth:`add_argument` for details.
13281330

1329-
By default, the argument strings are taken from :data:`sys.argv`, and a new empty
1330-
:class:`Namespace` object is created for the attributes.
1331+
* args_ - List of strings to parse. The default is taken from
1332+
:data:`sys.argv`.
1333+
1334+
* namespace_ - An object to take the attributes. The default is a new empty
1335+
:class:`Namespace` object.
13311336

13321337

13331338
Option value syntax
@@ -1469,6 +1474,7 @@ unambiguous (the prefix matches a unique option)::
14691474
An error is produced for arguments that could produce more than one options.
14701475
This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
14711476

1477+
.. _args:
14721478

14731479
Beyond ``sys.argv``
14741480
^^^^^^^^^^^^^^^^^^^
@@ -1490,6 +1496,7 @@ interactive prompt::
14901496
>>> parser.parse_args(['1', '2', '3', '4', '--sum'])
14911497
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
14921498

1499+
.. _namespace:
14931500

14941501
The Namespace object
14951502
^^^^^^^^^^^^^^^^^^^^
@@ -2010,7 +2017,12 @@ A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
20102017
* Replace ``(options, args) = parser.parse_args()`` with ``args =
20112018
parser.parse_args()`` and add additional :meth:`ArgumentParser.add_argument`
20122019
calls for the positional arguments. Keep in mind that what was previously
2013-
called ``options``, now in :mod:`argparse` context is called ``args``.
2020+
called ``options``, now in the :mod:`argparse` context is called ``args``.
2021+
2022+
* Replace :meth:`optparse.OptionParser.disable_interspersed_args`
2023+
by setting ``nargs`` of a positional argument to `argparse.REMAINDER`_, or
2024+
use :meth:`~ArgumentParser.parse_known_args` to collect unparsed argument
2025+
strings in a separate list.
20142026

20152027
* Replace callback actions and the ``callback_*`` keyword arguments with
20162028
``type`` or ``action`` arguments.

0 commit comments

Comments
 (0)