Skip to content

Commit 33c4136

Browse files
[3.13] gh-125254: Fix error report about ambiguous option in argparse (GH-125273) (GH-125359)
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 <[email protected]>
1 parent b4c504d commit 33c4136

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,7 @@ def consume_optional(start_index):
20112011
if len(option_tuples) > 1:
20122012
options = ', '.join([option_string
20132013
for action, option_string, sep, explicit_arg in option_tuples])
2014-
args = {'option': arg_string, 'matches': options}
2014+
args = {'option': arg_strings[start_index], 'matches': options}
20152015
msg = _('ambiguous option: %(option)s could match %(matches)s')
20162016
raise ArgumentError(None, msg % args)
20172017

Lib/test/test_argparse.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6680,9 +6680,19 @@ def test_conflicting_mutually_exclusive_args_zero_or_more_with_metavar2(self):
66806680
def test_ambiguous_option(self):
66816681
self.parser.add_argument('--foobaz')
66826682
self.parser.add_argument('--fooble', action='store_true')
6683+
self.parser.add_argument('--foogle')
66836684
self.assertRaisesRegex(argparse.ArgumentError,
6684-
"ambiguous option: --foob could match --foobaz, --fooble",
6685-
self.parser.parse_args, ['--foob'])
6685+
"ambiguous option: --foob could match --foobaz, --fooble",
6686+
self.parser.parse_args, ['--foob'])
6687+
self.assertRaisesRegex(argparse.ArgumentError,
6688+
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
6689+
self.parser.parse_args, ['--foob=1'])
6690+
self.assertRaisesRegex(argparse.ArgumentError,
6691+
"ambiguous option: --foob could match --foobaz, --fooble$",
6692+
self.parser.parse_args, ['--foob', '1', '--foogle', '2'])
6693+
self.assertRaisesRegex(argparse.ArgumentError,
6694+
"ambiguous option: --foob=1 could match --foobaz, --fooble$",
6695+
self.parser.parse_args, ['--foob=1', '--foogle', '2'])
66866696

66876697
def test_os_error(self):
66886698
self.parser.add_argument('file')
Lines changed: 1 addition & 0 deletions
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)