Skip to content

Commit 49e105f

Browse files
gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340)
1 parent d08c788 commit 49e105f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2328,7 +2328,7 @@ def _get_option_tuples(self, option_string):
23282328
action = self._option_string_actions[option_string]
23292329
tup = action, option_string, '', short_explicit_arg
23302330
result.append(tup)
2331-
elif option_string.startswith(option_prefix):
2331+
elif self.allow_abbrev and option_string.startswith(option_prefix):
23322332
action = self._option_string_actions[option_string]
23332333
tup = action, option_string, None, None
23342334
result.append(tup)

Lib/test/test_argparse.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,23 @@ class TestOptionalsDisallowLongAbbreviationPrefixChars(ParserTestCase):
914914
]
915915

916916

917+
class TestOptionalsDisallowSingleDashLongAbbreviation(ParserTestCase):
918+
"""Do not allow abbreviations of long options at all"""
919+
920+
parser_signature = Sig(allow_abbrev=False)
921+
argument_signatures = [
922+
Sig('-foo'),
923+
Sig('-foodle', action='store_true'),
924+
Sig('-foonly'),
925+
]
926+
failures = ['-foon 3', '-food', '-food -foo 2']
927+
successes = [
928+
('', NS(foo=None, foodle=False, foonly=None)),
929+
('-foo 3', NS(foo='3', foodle=False, foonly=None)),
930+
('-foonly 7 -foodle -foo 2', NS(foo='2', foodle=True, foonly='7')),
931+
]
932+
933+
917934
class TestDisallowLongAbbreviationAllowsShortGrouping(ParserTestCase):
918935
"""Do not allow abbreviations of long options at all"""
919936

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix disallowing abbreviation of single-dash long options in :mod:`argparse`
2+
with ``allow_abbrev=False``.

0 commit comments

Comments
 (0)