https://github.com/python/cpython/commit/f46d8475749a0eadbc1f37079906a8e1ed5d56dc
commit: f46d8475749a0eadbc1f37079906a8e1ed5d56dc
branch: main
author: Beomsoo Kim <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-11-26T10:54:02+02:00
summary:

gh-126946: Improve error message in getopt.do_longs based on existing comment 
(GH-126871)

Include a list of possibilities for not unique prefix.

files:
A Misc/NEWS.d/next/Library/2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst
M Lib/getopt.py
M Lib/test/translationdata/getopt/msgids.txt

diff --git a/Lib/getopt.py b/Lib/getopt.py
index a9c452a601ee81..25f3e2439b3e35 100644
--- a/Lib/getopt.py
+++ b/Lib/getopt.py
@@ -185,11 +185,13 @@ def long_has_args(opt, longopts):
         return True, opt
     elif opt + '=?' in possibilities:
         return '?', opt
-    # No exact match, so better be unique.
+    # Possibilities must be unique to be accepted
     if len(possibilities) > 1:
-        # XXX since possibilities contains all valid continuations, might be
-        # nice to work them into the error msg
-        raise GetoptError(_('option --%s not a unique prefix') % opt, opt)
+        raise GetoptError(
+            _("option --%s not a unique prefix; possible options: %s")
+            % (opt, ", ".join(possibilities)),
+            opt,
+        )
     assert len(possibilities) == 1
     unique_match = possibilities[0]
     if unique_match.endswith('=?'):
diff --git a/Lib/test/translationdata/getopt/msgids.txt 
b/Lib/test/translationdata/getopt/msgids.txt
index 1ffab1f31abad5..5c0c02d452d156 100644
--- a/Lib/test/translationdata/getopt/msgids.txt
+++ b/Lib/test/translationdata/getopt/msgids.txt
@@ -1,6 +1,6 @@
 option -%s not recognized
 option -%s requires argument
 option --%s must not have an argument
-option --%s not a unique prefix
+option --%s not a unique prefix; possible options: %s
 option --%s not recognized
 option --%s requires argument
\ No newline at end of file
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst 
b/Misc/NEWS.d/next/Library/2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst
new file mode 100644
index 00000000000000..448055ccfdff40
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst
@@ -0,0 +1,3 @@
+Improve the :exc:`~getopt.GetoptError` error message when a long option
+prefix matches multiple accepted options in :func:`getopt.getopt` and
+:func:`getopt.gnu_getopt`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to