Nested command option shadowing -h prints wrong suggestion
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
Please include the function and decorators that you used.
See https://github.com/tumidi/click-subcommand-help-message.
It's all in the repro. What else do you need?
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
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.
Thank you. We will take a look at the issue.
Potential duplicate of #2819