click icon indicating copy to clipboard operation
click copied to clipboard

Nested command option shadowing -h prints wrong suggestion

Open tumidi opened this issue 1 year ago • 6 comments

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

tumidi avatar Oct 21 '24 16:10 tumidi

Please include the function and decorators that you used.

Rowlando13 avatar Apr 06 '25 00:04 Rowlando13

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

It's all in the repro. What else do you need?

tumidi avatar Apr 06 '25 14:04 tumidi

It's much better for the long term to include the full code directly in the issue. That way we can look at old issues and have the full context

Rowlando13 avatar Apr 06 '25 18:04 Rowlando13

It's much better for the long term to include the full code directly in the issue. That way we can look at old issues and have the full context

I added the relevant code to the original issue text.

tumidi avatar Apr 07 '25 14:04 tumidi

Thank you. We will take a look at the issue.

Rowlando13 avatar Apr 07 '25 22:04 Rowlando13

Potential duplicate of #2819

Rowlando13 avatar Apr 07 '25 22:04 Rowlando13