]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-126946: Improve error message in getopt.do_longs based on existing comment (GH...
authorBeomsoo Kim <beoms424@gmail.com>
Tue, 26 Nov 2024 08:54:02 +0000 (17:54 +0900)
committerGitHub <noreply@github.com>
Tue, 26 Nov 2024 08:54:02 +0000 (10:54 +0200)
Include a list of possibilities for not unique prefix.

Lib/getopt.py
Lib/test/translationdata/getopt/msgids.txt
Misc/NEWS.d/next/Library/2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst [new file with mode: 0644]

index a9c452a601ee816f790521f4f425e870d5a741f3..25f3e2439b3e352488d5e2f7879d9627b7aa3a30 100644 (file)
@@ -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('=?'):
index 1ffab1f31abad5e5bbc42e63001c34d8c3463d6d..5c0c02d452d15654f0f83f5fa6d4e06d48e7c69f 100644 (file)
@@ -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 (file)
index 0000000..448055c
--- /dev/null
@@ -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`.