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 return True, opt
elif opt + '=?' in possibilities: elif opt + '=?' in possibilities:
return '?', opt return '?', opt
# No exact match, so better be unique. # Possibilities must be unique to be accepted
if len(possibilities) > 1: if len(possibilities) > 1:
# XXX since possibilities contains all valid continuations, might be raise GetoptError(
# nice to work them into the error msg _("option --%s not a unique prefix; possible options: %s")
raise GetoptError(_('option --%s not a unique prefix') % opt, opt) % (opt, ", ".join(possibilities)),
opt,
)
assert len(possibilities) == 1 assert len(possibilities) == 1
unique_match = possibilities[0] unique_match = possibilities[0]
if unique_match.endswith('=?'): if unique_match.endswith('=?'):

View file

@ -1,6 +1,6 @@
option -%s not recognized option -%s not recognized
option -%s requires argument option -%s requires argument
option --%s must not have an 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 not recognized
option --%s requires argument 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`.