fix(parser): preserve full option string in short-option error messages#3421
Closed
algojogacor wants to merge 1 commit into
Closed
fix(parser): preserve full option string in short-option error messages#3421algojogacor wants to merge 1 commit into
algojogacor wants to merge 1 commit into
Conversation
When a user passes an invalid multi-character single-dash option like '-dbgwrong', Click's parser splits it character-by-character as stacked short options and raises NoSuchOption with only the first unmatched single character (e.g. '-d'), producing the confusing error message 'No such option: -d'. This happens because _match_short_opt iterates over each character after the prefix and raises NoSuchOption with the single-char opt when a character is not found. When the very first character of the token fails to match, the user most likely intended the entire token as a single option name (e.g. -dbgwrong is a typo for -dbg). Fix: When the first character (i == 2) is not a registered short option, raise NoSuchOption with the full original argument string and include _long_opt possibilities for close-match suggestions. When a later character fails (genuine stacking error), keep the per-character diagnostic unchanged. Now '-dbgwrong' produces: No such option '-dbgwrong'. Did you mean '-dbg'? instead of: No such option: -d. Fixes pallets#2779
Member
|
AI junk, didn't read the linked discussion. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2779 — When a user passes an invalid multi-character single-dash option like
-dbgwrong, Click now shows the full original token in the error message with close-match suggestions (e.g.No such option '-dbgwrong'. Did you mean '-dbg'?) instead of the confusing single-character error (No such option: -d).Root Cause
In
src/click/parser.py, the_match_short_optmethod (line 390) iterates character-by-character over a single-dash argument. When a character is not found in_short_opt, it raisesNoSuchOption(opt, ...)whereoptis the single-character option like-d(line 405). This is technically correct for POSIX-style stacked short options (-abc=-a -b -c), but produces a confusing error when the user intended the entire token as a single option name (e.g.-dbgwrongis a typo for-dbg).The specific mechanism:
_process_optsreceives-dbgwrong, tries_match_long_optwhich fails (no exact match for-dbgwrong)_match_short_optwhich processesd,b,g...dis not a registered short option → raisesNoSuchOption(-d)— confusing!Fix
In
_match_short_opt, when the first character after the prefix is not a registered short option (i == 2), raiseNoSuchOptionwith the full originalargstring and include_long_optpossibilities forget_close_matchessuggestions. When a later character fails (genuine stacking error like-abZwhere-aand-bare valid but-Zis not), keep the existing per-character diagnostic.Changes
src/click/parser.py(lines 405-415): Added conditional in_match_short_optto detect first-character failure and raise with the full original argument + long-option suggestions.Testing
-dbgwrongwith-dbgdefined →No such option '-dbgwrong'. Did you mean '-dbg'?✓-xinvalidwith-ddefined →No such option '-xinvalid'.✓-abZwith-a/-bdefined (stacking, later char unknown) →No such option '-Z'.(per-char preserved) ✓-abstacking with-a/-bdefined → works correctly ✓--debgutypo suggestion →No such option '--debgu'. Did you mean '--debug'?✓