summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/argparse.py2
-rw-r--r--Lib/test/test_argparse.py14
-rw-r--r--Misc/NEWS.d/next/Library/2024-10-10-19-57-35.gh-issue-125254.RtZxXS.rst1
3 files changed, 14 insertions, 3 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 64dbd71..cbecb3b 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2019,7 +2019,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
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)
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 61ddb5f..1fc97de 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -6730,9 +6730,19 @@ class TestExitOnError(TestCase):
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')
diff --git a/Misc/NEWS.d/next/Library/2024-10-10-19-57-35.gh-issue-125254.RtZxXS.rst b/Misc/NEWS.d/next/Library/2024-10-10-19-57-35.gh-issue-125254.RtZxXS.rst
new file mode 100644
index 0000000..abe37fe
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-10-19-57-35.gh-issue-125254.RtZxXS.rst
@@ -0,0 +1 @@
+Fix a bug where ArgumentError includes the incorrect ambiguous option in :mod:`argparse`.