Skip to content

Nested command option shadowing -h prints wrong suggestion #2790

@tumidi

Description

@tumidi

Click prints an incorrect suggestion when using a combination of nested command and required argument. This happens when the nested command shadows the -h option shortcut of the main command which Click apparently tolerates.

Replication

See https://github.com/tumidi/click-subcommand-help-message.

$ click-test-cli foo
Usage: click-test-cli foo [OPTIONS] REQUIRED_ARG
Try 'click-test-cli foo -h' for help.

Error: Missing argument 'REQUIRED_ARG'.

But, actually running the suggested click-test-cli foo -h prints

$ click-test-cli foo -h
Error: Option '-h' requires an argument.

Interestingly, Click correctly detects the shadowing of the option when printing the foo command help and prints a consistent help page.

$ click-test-cli foo --help
Usage: click-test-cli foo [OPTIONS] REQUIRED_ARG

Options:
  -h, --host TEXT
  --help           Show this message and exit.

Python code

@click.group(context_settings={"help_option_names": ["-h", "--help"]})
def cli():
    pass


@click.command()
@click.argument("required_arg")
@click.option("--host", "-h", "host", default="localhost")
def foo(required_arg, host):
    click.echo(f"Foo command required_arg={required_arg} host={host}")


cli.add_command(foo)

Possible solutions

Fix the generated message, so it prints Try 'click-test-cli foo --help' for help. in this case.

Environment:

  • Python version: 3.12.6
  • Click version: 8.2.0.dev0 d73083e

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions