mirror of
https://github.com/python/cpython.git
synced 2025-08-08 10:58:51 +00:00
[3.12] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) (GH-124750)
(cherry picked from commit 49e105f948
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
65103adadd
commit
00fd32af21
3 changed files with 20 additions and 1 deletions
|
@ -2361,7 +2361,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
||||||
action = self._option_string_actions[option_string]
|
action = self._option_string_actions[option_string]
|
||||||
tup = action, option_string, '', short_explicit_arg
|
tup = action, option_string, '', short_explicit_arg
|
||||||
result.append(tup)
|
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]
|
action = self._option_string_actions[option_string]
|
||||||
tup = action, option_string, None, None
|
tup = action, option_string, None, None
|
||||||
result.append(tup)
|
result.append(tup)
|
||||||
|
|
|
@ -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):
|
class TestDisallowLongAbbreviationAllowsShortGrouping(ParserTestCase):
|
||||||
"""Do not allow abbreviations of long options at all"""
|
"""Do not allow abbreviations of long options at all"""
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix disallowing abbreviation of single-dash long options in :mod:`argparse`
|
||||||
|
with ``allow_abbrev=False``.
|
Loading…
Add table
Add a link
Reference in a new issue