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

Skip to content

Commit f311453

Browse files
committed
Issue #19814: Clarify argparse's docs w.r.t prefix matching
1 parent 22d0418 commit f311453

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Doc/library/argparse.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,12 +1371,14 @@ argument::
13711371
>>> parser.parse_args(['--', '-f'])
13721372
Namespace(foo='-f', one=None)
13731373

1374+
.. _prefix-matching:
13741375

1375-
Argument abbreviations
1376-
^^^^^^^^^^^^^^^^^^^^^^
1376+
Argument abbreviations (prefix matching)
1377+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13771378

13781379
The :meth:`~ArgumentParser.parse_args` method allows long options to be
1379-
abbreviated if the abbreviation is unambiguous::
1380+
abbreviated to a prefix, if the abbreviation is unambiguous (the prefix matches
1381+
a unique option)::
13801382

13811383
>>> parser = argparse.ArgumentParser(prog='PROG')
13821384
>>> parser.add_argument('-bacon')
@@ -1852,6 +1854,12 @@ the populated namespace and the list of remaining argument strings.
18521854
>>> parser.parse_known_args(['--foo', '--badger', 'BAR', 'spam'])
18531855
(Namespace(bar='BAR', foo=True), ['--badger', 'spam'])
18541856

1857+
.. warning::
1858+
:ref:`Prefix matching <prefix-matching>` rules apply to
1859+
:meth:`parse_known_args`. The parser may consume an option even if it's just
1860+
a prefix of one of its known options, instead of leaving it in the remaining
1861+
arguments list.
1862+
18551863

18561864
Customizing file parsing
18571865
^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)