Skip to content

Commit

Permalink
bpo-26967: fix flag grouping with allow_abbrev=False (pythonGH-14316)
Browse files Browse the repository at this point in the history
The `allow_abbrev` option for ArgumentParser is documented and intended to disable support for unique prefixes of --options, which may sometimes be ambiguous due to deferred parsing.

However, the initial implementation also broke parsing of grouped short flags, such as `-ab` meaning `-a -b` (or `-a=b`).  Checking the argument for a leading `--` before rejecting it fixes this.

This was prompted by pytest-dev/pytestGH-5469, so a backport to at least 3.8 would be great 😄
And this is my first PR to CPython, so please let me know if I've missed anything!

https://bugs.python.org/issue26967
(cherry picked from commit dffca9e)

Co-authored-by: Zac Hatfield-Dodds <Zac-HD@users.noreply.github.com>
  • Loading branch information
Zac-HD authored and miss-islington committed Jul 14, 2019
1 parent b815669 commit 9bfd3a2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Doc/library/argparse.rst
Expand Up @@ -182,6 +182,10 @@ ArgumentParser objects
.. versionchanged:: 3.5
*allow_abbrev* parameter was added.

.. versionchanged:: 3.8
In previous versions, *allow_abbrev* also disabled grouping of short
flags such as ``-vv`` to mean ``-v -v``.

The following sections describe how each of these are used.


Expand Down
2 changes: 1 addition & 1 deletion Lib/argparse.py
Expand Up @@ -2132,7 +2132,7 @@ def _parse_optional(self, arg_string):
action = self._option_string_actions[option_string]
return action, option_string, explicit_arg

if self.allow_abbrev:
if self.allow_abbrev or not arg_string.startswith('--'):
# search through all possible prefixes of the option string
# and all actions in the parser for possible interpretations
option_tuples = self._get_option_tuples(arg_string)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_argparse.py
Expand Up @@ -785,6 +785,25 @@ class TestOptionalsDisallowLongAbbreviation(ParserTestCase):
('--foonly 7 --foodle --foo 2', NS(foo='2', foodle=True, foonly='7')),
]


class TestDisallowLongAbbreviationAllowsShortGrouping(ParserTestCase):
"""Do not allow abbreviations of long options at all"""

parser_signature = Sig(allow_abbrev=False)
argument_signatures = [
Sig('-r'),
Sig('-c', action='count'),
]
failures = ['-r', '-c -r']
successes = [
('', NS(r=None, c=None)),
('-ra', NS(r='a', c=None)),
('-rcc', NS(r='cc', c=None)),
('-cc', NS(r=None, c=2)),
('-cc -ra', NS(r='a', c=2)),
('-ccrcc', NS(r='cc', c=2)),
]

# ================
# Positional tests
# ================
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Expand Up @@ -641,6 +641,7 @@ Travis B. Hartwell
Shane Harvey
Larry Hastings
Tim Hatch
Zac Hatfield-Dodds
Shane Hathaway
Michael Haubenwallner
Janko Hauser
Expand Down
@@ -0,0 +1,3 @@
An :class:`~argparse.ArgumentParser` with ``allow_abbrev=False`` no longer
disables grouping of short flags, such as ``-vv``, but only disables
abbreviation of long flags as documented. Patch by Zac Hatfield-Dodds.

0 comments on commit 9bfd3a2

Please sign in to comment.