Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Loading