Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-26967: fix flag grouping with allow_abbrev=False #14316

Merged
merged 1 commit into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ ArgumentParser objects
.. versionchanged:: 3.5
*allow_abbrev* parameter was added.

.. versionchanged:: 3.8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. versionchanged:: 3.8
.. versionchanged:: 3.9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still hoping that this will be merged into the 3.8 branch before 3.8.0, but I guess we can adjust the number later.

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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ================
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.