8000 gh-124345: Support abbreviated single-dash long options with = in argparse by serhiy-storchaka · Pull Request #124428 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-124345: Support abbreviated single-dash long options with = in argparse #124428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2318,7 +2318,9 @@ def _get_option_tuples(self, option_string):
# but multiple character options always have to have their argument
# separate
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_explicit_arg = option_string[2:]

Expand All @@ -2329,7 +2331,7 @@ def _get_option_tuples(self, option_string):
result.append(tup)
elif option_string.startswith(option_prefix):
action = self._option_string_actions[option_string]
tup = action, option_string, None, None
tup = action, option_string, sep, explicit_arg
result.append(tup)

# shouldn't ever get here
Expand Down
8000
11 changes: 10 additions & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,22 @@ class TestOptionalsSingleDashAmbiguous(ParserTestCase):
"""Test Optionals that partially match but are not subsets"""

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 = [
('', NS(foobar=None, 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')),
('-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')),
('-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')),
]


Expand Down Expand Up @@ -873,7 +880,9 @@ class TestOptionalsAllowLongAbbreviation(ParserTestCase):
successes = [
('', 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)),
('--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)),
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`argparse` vim supports abbreviated single-dash long options separated
by ``=`` from its value.
Loading
0