Skip to content

Add NoSuchCommand exception with suggestions for misspelled commands#3228

Merged
AndreasBackx merged 2 commits into
pallets:mainfrom
RC2215:no-such-command-possibilities
Apr 29, 2026
Merged

Add NoSuchCommand exception with suggestions for misspelled commands#3228
AndreasBackx merged 2 commits into
pallets:mainfrom
RC2215:no-such-command-possibilities

Conversation

@RC2215

@RC2215 RC2215 commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

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.

@kdeldycke kdeldycke changed the base branch from main to stable April 16, 2026 09:35
@Rowlando13 Rowlando13 added this to the 8.4.0 milestone Apr 29, 2026
@Rowlando13 Rowlando13 requested a review from kdeldycke April 29, 2026 06:08
@Rowlando13 Rowlando13 changed the base branch from stable to main April 29, 2026 06:52
@AndreasBackx AndreasBackx force-pushed the no-such-command-possibilities branch from ef89f1f to f6da6a7 Compare April 29, 2026 23:38
@AndreasBackx

Copy link
Copy Markdown
Collaborator

Thanks again for this nice QoL, apologies for the delay!

@AndreasBackx AndreasBackx merged commit 831c8f0 into pallets:main Apr 29, 2026
11 checks passed
@kdeldycke kdeldycke changed the title Add NoSuchCommand exception with suggestions for misspelled commands Add NoSuchCommand exception with suggestions for misspelled commands Apr 30, 2026
@kdeldycke kdeldycke added bug f:help feature: help text docs and removed bug labels Apr 30, 2026
@kdeldycke kdeldycke linked an issue Apr 30, 2026 that may be closed by this pull request
@kdeldycke kdeldycke requested a review from janluke April 30, 2026 10:54
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

docs f:help feature: help text

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest Did you mean for misspelled commands Provide suggestions for misspelled subcommands

4 participants