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

Include a list of possibilities for not unique prefix.
This commit is contained in:
Beomsoo Kim 2024-11-26 17:54:02 +09:00 committed by GitHub
parent 733fe59206
commit f46d847574
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 5 deletions

View 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('=?'):

View 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

View file

@ -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`.