Skip to content
Myr

Use non-shadowed help option name in error hint

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

Fix #2790.

authored by Kevin Deldycke · committed by Kevin Deldycke · parent f1f191e · 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.

Trailers

  • Co-authored-by: Mr-Neutr0n <64578610+Mr-Neutr0n@users.noreply.github.com>

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
38 unmodified lines
39 :issue:`2865`40- Change :class:`ParameterSource` to an :class:`~enum.IntEnum` and reorder41 its members from most to least explicit, so values can be compared to42 check whether a parameter was explicitly provided. :issue:`2879` :pr:`3248`43- The error hint now uses :meth:`Command.get_help_option_names` to pick44 non-shadowed help option names, so ``Try '... -h'`` no longer points to a45 subcommand option that shadows ``-h``. All surviving names are shown46 (``-h/--help``). :issue:`2790` :pr:`3208`4748Version 8.3.249-------------50
1323 unmodified lines