Skip to content

[3.13] gh-125254: Fix error report about ambiguous option in argparse (GH-125273) #125359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,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 @@ -6680,9 +6680,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`.
Loading