From bca0e40d5f4be8228acc158e07fbd43697c79f5f Mon Sep 17 00:00:00 2001 From: Zac-HD Date: Sun, 23 Jun 2019 13:26:39 +1000 Subject: [PATCH] bpo-26967: fix argparse flag grouping with allow_abbrev=False 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`. Checking for a leading `--` fixes this. --- Doc/library/argparse.rst | 4 ++++ Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 19 +++++++++++++++++++ Misc/ACKS | 1 + .../2019-06-23-12-46-10.bpo-26967.xEuem1.rst | 3 +++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-06-23-12-46-10.bpo-26967.xEuem1.rst diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index b77a38ccd48577..ef2fd42783c877 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -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. diff --git a/Lib/argparse.py b/Lib/argparse.py index 4f3aea928bf6f4..e45b67b67704a9 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2130,7 +2130,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) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index bcf2cc9b26a319..765fdb99f9c8cb 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -786,6 +786,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 # ================ diff --git a/Misc/ACKS b/Misc/ACKS index 05a3e61f694149..611fcad4a23a7f 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -641,6 +641,7 @@ Travis B. Hartwell Shane Harvey Larry Hastings Tim Hatch +Zac Hatfield-Dodds Shane Hathaway Michael Haubenwallner Janko Hauser diff --git a/Misc/NEWS.d/next/Library/2019-06-23-12-46-10.bpo-26967.xEuem1.rst b/Misc/NEWS.d/next/Library/2019-06-23-12-46-10.bpo-26967.xEuem1.rst new file mode 100644 index 00000000000000..c5852f6142150d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-23-12-46-10.bpo-26967.xEuem1.rst @@ -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.