Skip to content

Commit

Permalink
pythongh-125254: Fix error report about ambiguous option in argparse (p…
Browse files Browse the repository at this point in the history
…ythonGH-125273)

This was a regression introduced in pythongh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
  • Loading branch information
serhiy-storchaka authored Oct 12, 2024
1 parent 07c2d15 commit 63cf4e9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ def consume_optional(start_index):
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)

Expand Down
14 changes: 12 additions & 2 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6730,9 +6730,19 @@ def test_conflicting_mutually_exclusive_args_zero_or_more_with_metavar2(self):
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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where ArgumentError includes the incorrect ambiguous option in :mod:`argparse`.

0 comments on commit 63cf4e9

Please sign in to comment.