Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3e3e897

Browse filesBrowse files
authored
bpo-9182: Add a section on specifying positional arguments (#31810)
1 parent 57da3ff commit 3e3e897
Copy full SHA for 3e3e897

File tree

Expand file treeCollapse file tree

2 files changed

+34
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-2
lines changed

‎Doc/howto/argparse.rst

Copy file name to clipboardExpand all lines: Doc/howto/argparse.rst
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,35 @@ Output:
664664
4^2 == 16
665665
666666
667+
.. _specifying-ambiguous-arguments:
668+
669+
Specifying ambiguous arguments
670+
------------------------------
671+
672+
When there is ambiguity in deciding whether an argument is positional or for an
673+
argument, ``--`` can be used to tell :meth:`~ArgumentParser.parse_args` that
674+
everything after that is a positional argument::
675+
676+
>>> parser = argparse.ArgumentParser(prog='PROG')
677+
>>> parser.add_argument('-n', nargs='+')
678+
>>> parser.add_argument('args', nargs='*')
679+
680+
>>> # ambiguous, so parse_args assumes it's an option
681+
>>> parser.parse_args(['-f'])
682+
usage: PROG [-h] [-n N [N ...]] [args ...]
683+
PROG: error: unrecognized arguments: -f
684+
685+
>>> parser.parse_args(['--', '-f'])
686+
Namespace(args=['-f'], n=None)
687+
688+
>>> # ambiguous, so the -n option greedily accepts arguments
689+
>>> parser.parse_args(['-n', '1', '2', '3'])
690+
Namespace(args=[], n=['1', '2', '3'])
691+
692+
>>> parser.parse_args(['-n', '1', '--', '2', '3'])
693+
Namespace(args=['2', '3'], n=['1'])
694+
695+
667696
Conflicting options
668697
-------------------
669698

‎Doc/library/argparse.rst

Copy file name to clipboardExpand all lines: Doc/library/argparse.rst
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,8 @@ nargs
951951

952952
ArgumentParser objects usually associate a single command-line argument with a
953953
single action to be taken. The ``nargs`` keyword argument associates a
954-
different number of command-line arguments with a single action. The supported
955-
values are:
954+
different number of command-line arguments with a single action.
955+
See also :ref:`specifying-ambiguous-arguments`. The supported values are:
956956

957957
* ``N`` (an integer). ``N`` arguments from the command line will be gathered
958958
together into a list. For example::
@@ -1610,6 +1610,9 @@ argument::
16101610
>>> parser.parse_args(['--', '-f'])
16111611
Namespace(foo='-f', one=None)
16121612

1613+
See also :ref:`the argparse howto on ambiguous arguments <specifying-ambiguous-arguments>`
1614+
for more details.
1615+
16131616
.. _prefix-matching:
16141617

16151618
Argument abbreviations (prefix matching)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.