Skip to content

Comments

Add NoSuchCommand exception with suggestions for misspelled commands#3228

Open
RC2215 wants to merge 1 commit intopallets:mainfrom
RC2215:no-such-command-possibilities
Open

Add NoSuchCommand exception with suggestions for misspelled commands#3228
RC2215 wants to merge 1 commit intopallets:mainfrom
RC2215:no-such-command-possibilities

Conversation

@RC2215
Copy link

@RC2215 RC2215 commented Feb 23, 2026

This PR adds suggestions to the "No such command" error message for misspelled commands, similar to how it's done for NoSuchOption.

As part of this update, a new exception was added - NoSuchCommand, along with slight formatting changes in NoSuchOption, to improve readability and ensure consistency with other errors.

Rejected idea: Adding a shared base exception or mixin for NoSuchOption and NoSuchCommand.

Fixes #3107.

Additional behavior changes

  • The option possibilities are now calculated within the NoSuchOption exception.
  • The normalized command name is now used in the NoSuchCommand error message.

Example based on the issue

import click


@click.group()
def cli():
    pass


@cli.command()
@click.option("--name", default="World")
def greet(name: str) -> None:
    click.echo(f"Hello, {name}")


if __name__ == "__main__":
    cli()

Before the change:

$ ./main.py greet --nme David
Usage: main.py greet [OPTIONS]
Try 'main.py greet --help' for help.

Error: No such option: --nme Did you mean --name?
$ ./main.py gret --name David
Usage: main.py [OPTIONS] COMMAND [ARGS]...
Try 'main.py --help' for help.

Error: No such command 'gret'.

After the change:

$ ./main.py greet --nme David
Usage: main.py greet [OPTIONS]
Try 'main.py greet --help' for help.

Error: No such option '--nme'. Did you mean '--name'?
$ ./main.py gret --name David
Usage: main.py [OPTIONS] COMMAND [ARGS]...
Try 'main.py --help' for help.

Error: No such command 'gret'. Did you mean 'greet'?
  • Added unit tests for unknown and misspelled commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest "Did you mean" for misspelled commands

1 participant