Commit a38e31b4 authored by Steven Bethard's avatar Steven Bethard

Fix bug 9352 where characters were being lost in parsing some short options

parent 7728a1b8
...@@ -1794,13 +1794,13 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): ...@@ -1794,13 +1794,13 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
chars = self.prefix_chars chars = self.prefix_chars
if arg_count == 0 and option_string[1] not in chars: if arg_count == 0 and option_string[1] not in chars:
action_tuples.append((action, [], option_string)) action_tuples.append((action, [], option_string))
for char in self.prefix_chars: char = option_string[0]
option_string = char + explicit_arg[0] option_string = char + explicit_arg[0]
explicit_arg = explicit_arg[1:] or None new_explicit_arg = explicit_arg[1:] or None
optionals_map = self._option_string_actions optionals_map = self._option_string_actions
if option_string in optionals_map: if option_string in optionals_map:
action = optionals_map[option_string] action = optionals_map[option_string]
break explicit_arg = new_explicit_arg
else: else:
msg = _('ignored explicit argument %r') msg = _('ignored explicit argument %r')
raise ArgumentError(action, msg % explicit_arg) raise ArgumentError(action, msg % explicit_arg)
......
...@@ -465,6 +465,30 @@ class TestOptionalsAlternatePrefixCharsAddedHelp(ParserTestCase): ...@@ -465,6 +465,30 @@ class TestOptionalsAlternatePrefixCharsAddedHelp(ParserTestCase):
('/ba +f', NS(f=True, bar=None, baz=42)) ('/ba +f', NS(f=True, bar=None, baz=42))
] ]
class TestOptionalsAlternatePrefixCharsMultipleShortArgs(ParserTestCase):
"""Verify that Optionals must be called with their defined prefixes"""
parser_signature = Sig(prefix_chars='+-', add_help=False)
argument_signatures = [
Sig('-x', action='store_true'),
Sig('+y', action='store_true'),
Sig('+z', action='store_true'),
]
failures = ['-w',
'-xyz',
'+x',
'-y',
'+xyz',
]
successes = [
('', NS(x=False, y=False, z=False)),
('-x', NS(x=True, y=False, z=False)),
('+y -x', NS(x=True, y=True, z=False)),
('+yz -x', NS(x=True, y=True, z=True)),
]
class TestOptionalsShortLong(ParserTestCase): class TestOptionalsShortLong(ParserTestCase):
"""Test a combination of single- and double-dash option strings""" """Test a combination of single- and double-dash option strings"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment