Skip to content

Commit 63cf4e9

Browse files
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.
1 parent 07c2d15 commit 63cf4e9

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/argparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,7 @@ def consume_optional(start_index):
20192019
if len(option_tuples) > 1:
20202020
options = ', '.join([option_string
20212021
for action, option_string, sep, explicit_arg in option_tuples])
2022-
args = {'option': arg_string, 'matches': options}
2022+
args = {'option': arg_strings[start_index], 'matches': options}
20232023
msg = _('ambiguous option: %(option)s could match %(matches)s')
20242024
raise ArgumentError(None, msg % args)
20252025

Lib/test/test_argparse.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -6730,9 +6730,19 @@ def test_conflicting_mutually_exclusive_args_zero_or_more_with_metavar2(self):
67306730
def test_ambiguous_option(self):
67316731
self.parser.add_argument('--foobaz')
67326732
self.parser.add_argument('--fooble', action='store_true')
6733+
self.parser.add_argument('--foogle')
67336734
self.assertRaisesRegex(argparse.ArgumentError,
6734-
"ambiguous option: --foob could match --foobaz, --fooble",
6735-
self.parser.parse_args, ['--foob'])
6735+
"ambiguous option: --foob could match --foobaz, --fooble",
6736+
self.parser.parse_args, ['--foob'])
6737+
self.assertRaisesRegex(argparse.ArgumentError,
6738+
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
6739+
self.parser.parse_args, ['--foob=1'])
6740+
self.assertRaisesRegex(argparse.ArgumentError,
6741+
"ambiguous option: --foob could match --foobaz, --fooble$",
6742+
self.parser.parse_args, ['--foob', '1', '--foogle', '2'])
6743+
self.assertRaisesRegex(argparse.ArgumentError,
6744+
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
6745+
self.parser.parse_args, ['--foob=1', '--foogle', '2'])
67366746

67376747
def test_os_error(self):
67386748
self.parser.add_argument('file')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug where ArgumentError includes the incorrect ambiguous option in :mod:`argparse`.

0 commit comments

Comments
 (0)