Fix error message for unrecognized multicharacter short options#3253
Closed
lfshao wants to merge 1 commit intopallets:mainfrom
Closed
Fix error message for unrecognized multicharacter short options#3253lfshao wants to merge 1 commit intopallets:mainfrom
lfshao wants to merge 1 commit intopallets:mainfrom
Conversation
When a multicharacter short option like `-dbg` is registered and an unrecognized variant like `-dbgwrong` is passed, the parser first tries a long-option match (which fails), then falls back to character-by-character short-option matching. If the first character itself is not a known short option, the error previously reported only that character (e.g. `-d`) instead of the full original argument. Fix this by catching NoSuchOption in _process_opts when the short-option fallback fails on the very first character, and re-raising with the full original option name. Fixes pallets#2779
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.
Fixes #2779
When a multicharacter short option like
-dbgis registered (e.g.,@click.option("-dbg", is_flag=True)), the parser correctly handles it by treating it as a long-style option with a single-dash prefix. However, when an unrecognized variant like-dbgwrongis passed:_match_long_optfails because-dbgwrongis not a registered option._match_short_opt, which iterates character by character.-dis not a registered short option either, it raisesNoSuchOption("-d").This yields the confusing error:
No such option: -dinstead ofNo such option: -dbgwrong.Fix
In
_process_opts, wrap the_match_short_optfallback in a try/except. IfNoSuchOptionis raised for the very first character of the arg (i.e., the short-option parser never made any progress), re-raise with the full original option name instead.This preserves correct behaviour for normal short option combinations: if
-dand-bare registered but-gis not, passing-dbgstill correctly reportsNo such option: -g.Changes
src/click/parser.py: CatchNoSuchOptionfrom the short-option fallback and re-raise with the full option name when the first character itself is unknown.tests/test_parser.py: Add regression test for the fixed behaviour.CHANGES.rst: Add changelog entry.