Repository navigation
"Did you mean ...?" for misplaced options #1986
Replies: 2 comments
|
+1, this would be really nice. And since Typer now owns the vendored Click code, the hook point is pretty contained, so it seems like a good PR candidate. Where the current message comes from In if opt not in self._long_opt:
from difflib import get_close_matches
possibilities = get_close_matches(opt, self._long_opt)
raise NoSuchOption(opt, possibilities=possibilities, ctx=self.ctx)So the existing "Possible options: ..." hint only looks at the options of the current command, which is why What the change could look like Both raise sites already have def _find_owner_of_option(opt, ctx):
parent = ctx.parent if ctx else None
while parent is not None:
for param in parent.command.get_params(parent):
if opt in param.opts or opt in getattr(param, "secondary_opts", []):
return parent
parent = parent.parent
return Noneand when it finds one, build the hint from the command paths: A couple of details worth handling in a PR:
Since you already have a working handler and offered to open a PR, I'd go for it and link this discussion. A small PR with tests for nested groups ( |
|
Thanks both! The internals of passing context etc might still change in the upcoming refactors, so I would wait until those refactors are settled before considering a PR on this. |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Example Code
Description
Passing an option in the wrong position (ex: passing a callback option after a subcommand) currently displays
No such option: --name. This can be confusing, especially since many command line argument parsers allow options anywhere (including Python's built-inargparse).I currently use a custom exception handler that catches
click.NoSuchOption, and then traversesctxto see if any of the parent commands have a matching option. If so, it adds aDid you meansection indicating the proper option placement:Due to the
clickvendoring, this is no longer possible without accessing_click, so I'd like to suggest adding this totyperitself. (Happy to open a PR.)Operating System
Other
Operating System Details
No response
Project Version
0.27.3
Python Version
No response
Additional Context
#1832 (reply in thread)
All reactions