Skip to content
Myr

fix help hint showing shadowed option name (#3208)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Rowlando13 · committed by GitHub · parents 8d34495, 2d0490b · Browse files · 3 files+76 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+76 added−1 removed

  • tests:+67 added−0 removed
  • other:+9 added−1 removed

3 files+76 added−1 removed

modified:src/click/exceptions.py+5 added-1 removed
76 unmodified lines
77 if (78 self.ctx is not None79 and self.ctx.command.get_help_option(self.ctx) is not None80 ):81 help_names = self.ctx.command.get_help_option_names(self.ctx)82 # Pick the longest name (like ``--help`` over ``-h``) for83 # readability in error messages.84 hint = _("Try '{command} {option}' for help.").format(82 command=self.ctx.command_path, option=self.ctx.help_option_names[0]85 command=self.ctx.command_path,86 option=max(help_names, key=len),87 )88 hint = f"{hint}\n"89 if self.ctx is not None:90 color = self.ctx.color
222 unmodified lines
modified:tests/test_formatting.py+67 added
1import pytest23import click456def test_basic_functionality(runner):
240 unmodified lines
247 "Error: Missing argument 'ARG'.",248 ]249250251@pytest.mark.parametrize(252 ("help_names", "extra_options", "expected_hint"),253 [254 # No shadowing, longest name is picked.255 (["-h", "--help"], [], "Try 'cli foo --help' for help."),256 # -h shadowed by a subcommand option, --help still available.257 (258 ["-h", "--help"],259 [click.option("--host", "-h")],260 "Try 'cli foo --help' for help.",261 ),262 # --help shadowed, -h still available.263 (264 ["-h", "--help"],265 [click.option("--help-file", "--help")],266 "Try 'cli foo -h' for help.",267 ),268 # Both names shadowed: no hint line at all.269 (270 ["-h", "--help"],271 [click.option("--host", "-h"), click.option("--help-file", "--help")],272 None,273 ),274 # Single custom help name, not shadowed.275 (["--man"], [], "Try 'cli foo --man' for help."),276 # Three help names, one shadowed, longest survivor picked.277 (278 ["-h", "--help", "--info"],279 [click.option("--info-file", "--info")],280 "Try 'cli foo --help' for help.",281 ),282 ],283)284def test_formatting_usage_error_help_hint(285 runner, help_names, extra_options, expected_hint286):287 """The error hint should only show non-shadowed help option names,288 picking the longest for readability.289290 https://github.com/pallets/click/issues/2790291 """292293 @click.group(context_settings={"help_option_names": help_names})294 def cli():295 pass296297 @cli.command()298 @click.argument("required_arg")299 def foo(required_arg, **kwargs):300 pass301302 for option in extra_options:303 option(foo)304305 result = runner.invoke(cli, ["foo"])306 assert result.exit_code == 2307 lines = result.output.splitlines()308 assert lines[0] == "Usage: cli foo [OPTIONS] REQUIRED_ARG"309 assert lines[-1] == "Error: Missing argument 'REQUIRED_ARG'."310 if expected_hint is not None:311 assert expected_hint in lines312 else:313 assert not any(line.startswith("Try ") for line in lines)314315316def test_formatting_custom_type_metavar(runner):317 class MyType(click.ParamType):318 def get_metavar(self, param: click.Parameter, ctx: click.Context):319 return "MY_TYPE"
116 unmodified lines
modified:CHANGES.rst+4 added
23 unmodified lines
24 (not ``str``, ``int``, ``float``, or ``bool``), so programmer-provided25 Python objects like classes and enum members are passed through unchanged26 instead of being stringified. Previously ``type=click.UNPROCESSED`` had27 to be set explicitly. :issue:`2012` :pr:`3363`28- The error hint now uses :meth:`Command.get_help_option_names` to pick29 non-shadowed help option names, so ``Try '... -h'`` no longer points to a30 subcommand option that shadows ``-h``. All surviving names are shown31 (``-h/--help``). :issue:`2790` :pr:`3208`3233Version 8.3.334-------------35
1364 unmodified lines