Skip to content

Commit f46d847

Browse files
authored
gh-126946: Improve error message in getopt.do_longs based on existing comment (GH-126871)
Include a list of possibilities for not unique prefix.
1 parent 733fe59 commit f46d847

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

Lib/getopt.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,13 @@ def long_has_args(opt, longopts):
185185
return True, opt
186186
elif opt + '=?' in possibilities:
187187
return '?', opt
188-
# No exact match, so better be unique.
188+
# Possibilities must be unique to be accepted
189189
if len(possibilities) > 1:
190-
# XXX since possibilities contains all valid continuations, might be
191-
# nice to work them into the error msg
192-
raise GetoptError(_('option --%s not a unique prefix') % opt, opt)
190+
raise GetoptError(
191+
_("option --%s not a unique prefix; possible options: %s")
192+
% (opt, ", ".join(possibilities)),
193+
opt,
194+
)
193195
assert len(possibilities) == 1
194196
unique_match = possibilities[0]
195197
if unique_match.endswith('=?'):
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
option -%s not recognized
22
option -%s requires argument
33
option --%s must not have an argument
4-
option --%s not a unique prefix
4+
option --%s not a unique prefix; possible options: %s
55
option --%s not recognized
66
option --%s requires argument
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve the :exc:`~getopt.GetoptError` error message when a long option
2+
prefix matches multiple accepted options in :func:`getopt.getopt` and
3+
:func:`getopt.gnu_getopt`.

0 commit comments

Comments
 (0)