Skip to content

Commit aa0cdeb

Browse files
[3.12] gh-125254: Fix error report about ambiguous option in argparse (GH-125273) (GH-125360)
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. (cherry picked from commit 63cf4e9) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent affffc7 commit aa0cdeb

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
@@ -2025,7 +2025,7 @@ def consume_optional(start_index):
20252025
if len(option_tuples) > 1:
20262026
options = ', '.join([option_string
20272027
for action, option_string, sep, explicit_arg in option_tuples])
2028-
args = {'option': arg_string, 'matches': options}
2028+
args = {'option': arg_strings[start_index], 'matches': options}
20292029
msg = _('ambiguous option: %(option)s could match %(matches)s')
20302030
raise ArgumentError(None, msg % args)
20312031

Lib/test/test_argparse.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -6310,9 +6310,19 @@ def test_conflicting_mutually_exclusive_args_zero_or_more_with_metavar2(self):
63106310
def test_ambiguous_option(self):
63116311
self.parser.add_argument('--foobaz')
63126312
self.parser.add_argument('--fooble', action='store_true')
6313+
self.parser.add_argument('--foogle')
63136314
self.assertRaisesRegex(argparse.ArgumentError,
6314-
"ambiguous option: --foob could match --foobaz, --fooble",
6315-
self.parser.parse_args, ['--foob'])
6315+
"ambiguous option: --foob could match --foobaz, --fooble",
6316+
self.parser.parse_args, ['--foob'])
6317+
self.assertRaisesRegex(argparse.ArgumentError,
6318+
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
6319+
self.parser.parse_args, ['--foob=1'])
6320+
self.assertRaisesRegex(argparse.ArgumentError,
6321+
"ambiguous option: --foob could match --foobaz, --fooble$",
6322+
self.parser.parse_args, ['--foob', '1', '--foogle', '2'])
6323+
self.assertRaisesRegex(argparse.ArgumentError,
6324+
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
6325+
self.parser.parse_args, ['--foob=1', '--foogle', '2'])
63166326

63176327
def test_os_error(self):
63186328
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)