Implement suggestion for command typo#699
Merged
Merged
Conversation
xieofxie
reviewed
May 21, 2026
xieofxie
approved these changes
May 22, 2026
xieofxie
reviewed
May 22, 2026
xieofxie
approved these changes
May 22, 2026
DingmaomaoBJTU
pushed a commit
that referenced
this pull request
May 22, 2026
Fix #508 ## Suggest closest subcommand on typos (fixes #508) ### Before ``` $ winml exprt Usage: winml [OPTIONS] COMMAND [ARGS]... Try 'winml --help' for help. Error: No such command 'exprt'. ``` The bare error gives no recourse — users had to re-read `winml --help` to find the intended name. This forced a round-trip for what is almost always a one-character typo. ### After ``` $ winml exprt Usage: winml [OPTIONS] COMMAND [ARGS]... Try 'winml --help' for help. Error: No such command 'exprt'. Did you mean 'export'? ``` ### Fix Click 8.4 ships a built-in typo suggester: `Group.resolve_command` raises `NoSuchCommand(cmd_name, possibilities=self.commands, ctx=ctx)`, and `NoSuchCommand` runs `difflib.get_close_matches` over the keys of `possibilities` to append `Did you mean 'X'?` to the error message. The catch: `LazyGroup` discovers subcommands by scanning `src/winml/modelkit/commands/*.py` at runtime (via `list_commands`) and never eagerly populates `self.commands`. So Click had nothing to compare against. This PR is a 3-line adapter on top of Click's built-in path: ```python def resolve_command(self, ctx: click.Context, args: list[str]): """Seed ``self.commands`` so Click can emit a did-you-mean hint on typos.""" # Click's NoSuchCommand exception uses self.commands to find suggestions. for name in self.list_commands(ctx): self.commands.setdefault(name, None) # type: ignore[arg-type] return super().resolve_command(ctx, args)
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.
Fix #508
Suggest closest subcommand on typos (fixes #508)
Before
The bare error gives no recourse — users had to re-read
winml --helpto find the intended name. This forced a round-trip for what is almost always a one-character typo.After
Fix
Click 8.4 ships a built-in typo suggester:
Group.resolve_commandraisesNoSuchCommand(cmd_name, possibilities=self.commands, ctx=ctx), andNoSuchCommandrunsdifflib.get_close_matchesover the keys ofpossibilitiesto appendDid you mean 'X'?to the error message.The catch:
LazyGroupdiscovers subcommands by scanningsrc/winml/modelkit/commands/*.pyat runtime (vialist_commands) and never eagerly populatesself.commands. So Click had nothing to compare against.This PR is a 3-line adapter on top of Click's built-in path: