mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
[3.13] gh-124345: Support abbreviated single-dash long options with = in argparse (GH-124428) (GH-124753)
(cherry picked from commit 61180446ee
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
f28906e58e
commit
597b6211ab
3 changed files with 16 additions and 3 deletions
|
@ -2344,7 +2344,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
||||||
# but multiple character options always have to have their argument
|
# but multiple character options always have to have their argument
|
||||||
# separate
|
# separate
|
||||||
elif option_string[0] in chars and option_string[1] not in chars:
|
elif option_string[0] in chars and option_string[1] not in chars:
|
||||||
option_prefix = option_string
|
option_prefix, sep, explicit_arg = option_string.partition('=')
|
||||||
|
if not sep:
|
||||||
|
sep = explicit_arg = None
|
||||||
short_option_prefix = option_string[:2]
|
short_option_prefix = option_string[:2]
|
||||||
short_explicit_arg = option_string[2:]
|
short_explicit_arg = option_string[2:]
|
||||||
|
|
||||||
|
@ -2355,7 +2357,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
||||||
result.append(tup)
|
result.append(tup)
|
||||||
elif self.allow_abbrev and 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, sep, explicit_arg
|
||||||
result.append(tup)
|
result.append(tup)
|
||||||
|
|
||||||
# shouldn't ever get here
|
# shouldn't ever get here
|
||||||
|
|
|
@ -380,15 +380,22 @@ class TestOptionalsSingleDashAmbiguous(ParserTestCase):
|
||||||
"""Test Optionals that partially match but are not subsets"""
|
"""Test Optionals that partially match but are not subsets"""
|
||||||
|
|
||||||
argument_signatures = [Sig('-foobar'), Sig('-foorab')]
|
argument_signatures = [Sig('-foobar'), Sig('-foorab')]
|
||||||
failures = ['-f', '-f a', '-fa', '-foa', '-foo', '-fo', '-foo b']
|
failures = ['-f', '-f a', '-fa', '-foa', '-foo', '-fo', '-foo b',
|
||||||
|
'-f=a', '-foo=b']
|
||||||
successes = [
|
successes = [
|
||||||
('', NS(foobar=None, foorab=None)),
|
('', NS(foobar=None, foorab=None)),
|
||||||
('-foob a', NS(foobar='a', foorab=None)),
|
('-foob a', NS(foobar='a', foorab=None)),
|
||||||
|
('-foob=a', NS(foobar='a', foorab=None)),
|
||||||
('-foor a', NS(foobar=None, foorab='a')),
|
('-foor a', NS(foobar=None, foorab='a')),
|
||||||
|
('-foor=a', NS(foobar=None, foorab='a')),
|
||||||
('-fooba a', NS(foobar='a', foorab=None)),
|
('-fooba a', NS(foobar='a', foorab=None)),
|
||||||
|
('-fooba=a', NS(foobar='a', foorab=None)),
|
||||||
('-foora a', NS(foobar=None, foorab='a')),
|
('-foora a', NS(foobar=None, foorab='a')),
|
||||||
|
('-foora=a', NS(foobar=None, foorab='a')),
|
||||||
('-foobar a', NS(foobar='a', foorab=None)),
|
('-foobar a', NS(foobar='a', foorab=None)),
|
||||||
|
('-foobar=a', NS(foobar='a', foorab=None)),
|
||||||
('-foorab a', NS(foobar=None, foorab='a')),
|
('-foorab a', NS(foobar=None, foorab='a')),
|
||||||
|
('-foorab=a', NS(foobar=None, foorab='a')),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -918,7 +925,9 @@ class TestOptionalsAllowLongAbbreviation(ParserTestCase):
|
||||||
successes = [
|
successes = [
|
||||||
('', NS(foo=None, foobaz=None, fooble=False)),
|
('', NS(foo=None, foobaz=None, fooble=False)),
|
||||||
('--foo 7', NS(foo='7', foobaz=None, fooble=False)),
|
('--foo 7', NS(foo='7', foobaz=None, fooble=False)),
|
||||||
|
('--foo=7', NS(foo='7', foobaz=None, fooble=False)),
|
||||||
('--fooba a', NS(foo=None, foobaz='a', fooble=False)),
|
('--fooba a', NS(foo=None, foobaz='a', fooble=False)),
|
||||||
|
('--fooba=a', NS(foo=None, foobaz='a', fooble=False)),
|
||||||
('--foobl --foo g', NS(foo='g', foobaz=None, fooble=True)),
|
('--foobl --foo g', NS(foo='g', foobaz=None, fooble=True)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:mod:`argparse` vim supports abbreviated single-dash long options separated
|
||||||
|
by ``=`` from its value.
|
Loading…
Add table
Add a link
Reference in a new issue