8000 [3.12] gh-104860: Fix allow_abbrev=False for single-dash long options… · python/cpython@00fd32a · GitHub
[go: up one dir, main page]

Skip to content

Commit 00fd32a

Browse files
[3.12] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) (GH-124750)
(cherry picked from commit 49e105f) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 65103ad commit 00fd32a

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
@@ -2361,7 +2361,7 @@ def _get_option_tuples(self, option_string):
23612361
action = self._option_string_actions[option_string]
23622362
tup = action, option_string, '', short_explicit_arg
23632363
result.append(tup)
2364-
elif option_string.startswith(option_prefix):
2364+
elif self.allow_abbrev and option_string.startswith(option_prefix):
23652365
action = self._option_string_actions[option_string]
23662366
tup = action, option_string, None, None
23672367
result.append(tup)

Lib/test/test_argparse.py

Lines changed: 17 additions & 0 deletions
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

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)
0