8000 [3.12] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) by miss-islington · Pull Request #124750 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.12] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) #124750

8000
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2361,7 +2361,7 @@ def _get_option_tuples(self, option_string):
action = self._option_string_actions[option_string]
tup = action, option_string, '', short_explicit_arg
result.append(tup)
elif option_string.startswith(option_prefix):
elif self.allow_abbrev and option_string.startswith(option_prefix):
action = self._option_string_actions[option_string]
tup = action, option_string, None, None
result.append(tup)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,23 @@ class TestOptionalsDisallowLongAbbreviationPrefixChars(ParserTestCase):
]


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

parser_signature = Sig(allow_abbrev=False)
argument_signatures = [
Sig('-foo'),
Sig('-foodle', action='store_true'),
Sig('-foonly'),
]
failures = ['-foon 3', '-food', '-food -foo 2']
successes = [
('', NS(foo=None, foodle=False, foonly=None)),
('-foo 3', NS(foo='3', foodle=False, foonly=None)),
('-foonly 7 -foodle -foo 2', NS(foo='2', foodle=True, foonly='7')),
]


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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix disallowing abbreviation of single-dash long options in :mod:`argparse`
with ``allow_abbrev=False``.
Loading
0