Skip to content

Commit aa648c2

Browse files
[3.13] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) (GH-124749)
(cherry picked from commit 49e105f) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 16127de commit aa648c2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/argparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2353,7 +2353,7 @@ def _get_option_tuples(self, option_string):
23532353
action = self._option_string_actions[option_string]
23542354
tup = action, option_string, '', short_explicit_arg
23552355
result.append(tup)
2356-
elif option_string.startswith(option_prefix):
2356+
elif self.allow_abbrev and option_string.startswith(option_prefix):
23572357
action = self._option_string_actions[option_string]
23582358
tup = action, option_string, None, None
23592359
result.append(tup)

Lib/test/test_argparse.py

+17
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,23 @@ class TestOptionalsDisallowLongAbbreviationPrefixChars(ParserTestCase):
957957
]
958958

959959

960+
class TestOptionalsDisallowSingleDashLongAbbreviation(ParserTestCase):
961+
"""Do not allow abbreviations of long options at all"""
962+
963+
parser_signature = Sig(allow_abbrev=False)
964+
argument_signatures = [
965+
Sig('-foo'),
966+
Sig('-foodle', action='store_true'),
967+
Sig('-foonly'),
968+
]
969+
failures = ['-foon 3', '-food', '-food -foo 2']
970+
successes = [
971+
('', NS(foo=None, foodle=False, foonly=None)),
972+
('-foo 3', NS(foo='3', foodle=False, foonly=None)),
973+
('-foonly 7 -foodle -foo 2', NS(foo='2', foodle=True, foonly='7')),
974+
]
975+
976+
960977
class TestDisallowLongAbbreviationAllowsShortGrouping(ParserTestCase):
961978
"""Do not allow abbreviations of long options at all"""
962979

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)