bpo-26967: fix flag grouping with allow_abbrev=False (GH-14316) (GH-14759)
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 :smile:
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 dffca9e925ee5c3072663cbe8d4d4768406d5307)
Co-authored-by: Zac Hatfield-Dodds <Zac-HD@users.noreply.github.com>
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 9a67b41..5820d0d 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2132,7 +2132,7 @@
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)