click-contrib / click-repl

Subcommand REPL for click apps
MIT License
215 stars 41 forks source link

Unhandled UsageError exception when unsupported command is provided #110

Open aittalam opened 7 months ago

aittalam commented 7 months ago

Hi, while developing a click-repl tool I realized that if I enter an undefined command I get a UsageError immediately after I hit the space bar. For instance:

python cli.py

<here I enter "whatever" followed by a space>

Unhandled exception in event loop:
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/prompt_toolkit/buffer.py", line 1919, in new_coroutine
    await coroutine(*a, **kw)
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/prompt_toolkit/buffer.py", line 1743, in async_completer
    async for completion in async_generator:
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/prompt_toolkit/completion/base.py", line 323, in get_completions_async
    async for completion in completer.get_completions_async(
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/prompt_toolkit/completion/base.py", line 199, in get_completions_async
    for item in self.get_completions(document, complete_event):
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/click_repl/_completer.py", line 257, in get_completions
    self.parsed_ctx = _resolve_context(args, self.ctx)
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/click_repl/utils.py", line 46, in _resolve_context
    name, cmd, args = command.resolve_command(ctx, args)
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/click/core.py", line 1746, in resolve_command
    ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))
  File "/Users/mala/envs/jup/lib/python3.9/site-packages/click/core.py", line 684, in fail
    raise UsageError(message, self)

Exception No such command 'whatever'.
Press ENTER to continue...
> whatever

I found that surrounding this with a try, catching the exception and setting cmd to None works fine for my personal usage (i.e. it lets me write anything and returns a "no such command" error only when I hit enter), but I am not sure whether this is a desired behavior how this would impact other parts of the repl. What are your suggestions to solve this issue?

thomask77 commented 5 months ago

Could this be a duplicate of #108 which was fixed in the merged PR #109 ?