Skip to content

Commit

Permalink
gh-126946: Improve error message in getopt.do_longs based on existing…
Browse files Browse the repository at this point in the history
… comment (GH-126871)

Include a list of possibilities for not unique prefix.
  • Loading branch information
bombs-kim authored Nov 26, 2024
1 parent 733fe59 commit f46d847
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Lib/getopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('=?'):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/translationdata/getopt/msgids.txt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit f46d847

Please sign in to comment.