From f46d8475749a0eadbc1f37079906a8e1ed5d56dc Mon Sep 17 00:00:00 2001 From: Beomsoo Kim Date: Tue, 26 Nov 2024 17:54:02 +0900 Subject: [PATCH] gh-126946: Improve error message in getopt.do_longs based on existing comment (GH-126871) Include a list of possibilities for not unique prefix. --- Lib/getopt.py | 10 ++++++---- Lib/test/translationdata/getopt/msgids.txt | 2 +- .../2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-11-18-16-43-11.gh-issue-126946.52Ou-B.rst 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`.