Skip to content

Commit 71a2b8d

Browse files
[3.12] gh-124345: Support abbreviated single-dash long options with = in argparse (GH-124428) (GH-124754)
(cherry picked from commit 6118044) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent bdcdb0a commit 71a2b8d

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Lib/argparse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,7 +2352,9 @@ def _get_option_tuples(self, option_string):
23522352
# but multiple character options always have to have their argument
23532353
# separate
23542354
elif option_string[0] in chars and option_string[1] not in chars:
2355-
option_prefix = option_string
2355+
option_prefix, sep, explicit_arg = option_string.partition('=')
2356+
if not sep:
2357+
sep = explicit_arg = None
23562358
short_option_prefix = option_string[:2]
23572359
short_explicit_arg = option_string[2:]
23582360

@@ -2363,7 +2365,7 @@ def _get_option_tuples(self, option_string):
23632365
result.append(tup)
23642366
elif self.allow_abbrev and option_string.startswith(option_prefix):
23652367
action = self._option_string_actions[option_string]
2366-
tup = action, option_string, None, None
2368+
tup = action, option_string, sep, explicit_arg
23672369
result.append(tup)
23682370

23692371
# shouldn't ever get here

Lib/test/test_argparse.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,22 @@ class TestOptionalsSingleDashAmbiguous(ParserTestCase):
380380
"""Test Optionals that partially match but are not subsets"""
381381

382382
argument_signatures = [Sig('-foobar'), Sig('-foorab')]
383-
failures = ['-f', '-f a', '-fa', '-foa', '-foo', '-fo', '-foo b']
383+
failures = ['-f', '-f a', '-fa', '-foa', '-foo', '-fo', '-foo b',
384+
'-f=a', '-foo=b']
384385
successes = [
385386
('', NS(foobar=None, foorab=None)),
386387
('-foob a', NS(foobar='a', foorab=None)),
388+
('-foob=a', NS(foobar='a', foorab=None)),
387389
('-foor a', NS(foobar=None, foorab='a')),
390+
('-foor=a', NS(foobar=None, foorab='a')),
388391
('-fooba a', NS(foobar='a', foorab=None)),
392+
('-fooba=a', NS(foobar='a', foorab=None)),
389393
('-foora a', NS(foobar=None, foorab='a')),
394+
('-foora=a', NS(foobar=None, foorab='a')),
390395
('-foobar a', NS(foobar='a', foorab=None)),
396+
('-foobar=a', NS(foobar='a', foorab=None)),
391397
('-foorab a', NS(foobar=None, foorab='a')),
398+
('-foorab=a', NS(foobar=None, foorab='a')),
392399
]
393400

394401

@@ -918,7 +925,9 @@ class TestOptionalsAllowLongAbbreviation(ParserTestCase):
918925
successes = [
919926
('', NS(foo=None, foobaz=None, fooble=False)),
920927
('--foo 7', NS(foo='7', foobaz=None, fooble=False)),
928+
('--foo=7', NS(foo='7', foobaz=None, fooble=False)),
921929
('--fooba a', NS(foo=None, foobaz='a', fooble=False)),
930+
('--fooba=a', NS(foo=None, foobaz='a', fooble=False)),
922931
('--foobl --foo g', NS(foo='g', foobaz=None, fooble=True)),
923932
]
924933

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`argparse` vim supports abbreviated single-dash long options separated
2+
by ``=`` from its value.

0 commit comments

Comments
 (0)