mirror of https://github.com/python/cpython
gh-125254: Fix error report about ambiguous option in argparse (GH-125273)
This was a regression introduced in gh-58573. It was only tested for the case when the ambiguous option is the last argument in the command line.
This commit is contained in:
parent
07c2d15977
commit
63cf4e914f
|
@ -2019,7 +2019,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
if len(option_tuples) > 1:
|
||||
options = ', '.join([option_string
|
||||
for action, option_string, sep, explicit_arg in option_tuples])
|
||||
args = {'option': arg_string, 'matches': options}
|
||||
args = {'option': arg_strings[start_index], 'matches': options}
|
||||
msg = _('ambiguous option: %(option)s could match %(matches)s')
|
||||
raise ArgumentError(None, msg % args)
|
||||
|
||||
|
|
|
@ -6730,9 +6730,19 @@ class TestExitOnError(TestCase):
|
|||
def test_ambiguous_option(self):
|
||||
self.parser.add_argument('--foobaz')
|
||||
self.parser.add_argument('--fooble', action='store_true')
|
||||
self.parser.add_argument('--foogle')
|
||||
self.assertRaisesRegex(argparse.ArgumentError,
|
||||
"ambiguous option: --foob could match --foobaz, --fooble",
|
||||
self.parser.parse_args, ['--foob'])
|
||||
"ambiguous option: --foob could match --foobaz, --fooble",
|
||||
self.parser.parse_args, ['--foob'])
|
||||
self.assertRaisesRegex(argparse.ArgumentError,
|
||||
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
|
||||
self.parser.parse_args, ['--foob=1'])
|
||||
self.assertRaisesRegex(argparse.ArgumentError,
|
||||
"ambiguous option: --foob could match --foobaz, --fooble$",
|
||||
self.parser.parse_args, ['--foob', '1', '--foogle', '2'])
|
||||
self.assertRaisesRegex(argparse.ArgumentError,
|
||||
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
|
||||
self.parser.parse_args, ['--foob=1', '--foogle', '2'])
|
||||
|
||||
def test_os_error(self):
|
||||
self.parser.add_argument('file')
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Fix a bug where ArgumentError includes the incorrect ambiguous option in :mod:`argparse`.
|
Loading…
Reference in New Issue