Skip to content
Myr

Merge pull request #1964 from janluke/fix/show_default-priority

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

authored by David Lord · committed by GitHub · parents 08ecfd0, 144f370 · Browse files · 3 files+56 added−16 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+56 added−16 removed

  • tests:+22 added−1 removed
  • other:+34 added−15 removed

3 files+56 added−16 removed

modified:src/click/core.py+32 added-15 removed
221 unmodified lines
222 default is autodetection. This is only needed if ANSI223 codes are used in texts that Click prints which is by224 default not the case. This for instance would affect225 help output.226 :param show_default: Show defaults for all options. If not set,227 defaults to the value from a parent context. Overrides an228 option's ``show_default`` argument.226 :param show_default: Show the default value for commands. If this227 value is not set, it defaults to the value from the parent228 context. ``Command.show_default`` overrides this default for the229 specific command.230231 .. versionchanged:: 8.1232 The ``show_default`` parameter is overridden by233 ``Command.show_default``, instead of the other way around.234235 .. versionchanged:: 8.0236 The ``show_default`` parameter defaults to the value from the237 parent context.238
2137 unmodified lines
2376 have some extra features that arguments don't have.23772378 All other parameters are passed onwards to the parameter constructor.23792375 :param show_default: Controls if the default value should be shown2376 on the help page. Normally, defaults are not shown. If this2377 value is a string, it shows that string in parentheses instead2378 of the actual value. This is particularly useful for dynamic2379 options. For single option boolean flags, the default remains2380 hidden if its value is ``False``.2380 :param show_default: Show the default value for this option in its2381 help text. Values are not shown by default, unless2382 :attr:`Context.show_default` is ``True``. If this value is a2383 string, it shows that string in parentheses instead of the2384 actual value. This is particularly useful for dynamic options.2385 For single option boolean flags, the default remains hidden if2386 its value is ``False``.2387 :param show_envvar: Controls if an environment variable should be2382 shown on the help page. Normally, environment ariables are not2388 shown on the help page. Normally, environment variables are not2389 shown.2390 :param prompt: If set to ``True`` or a non empty string then the2391 user will be prompted for input. If set to ``True`` the prompt2392 will be the option name capitalized.
22 unmodified lines
2415 :param help: the help string.2416 :param hidden: hide this option from help outputs.24172418 .. versionchanged:: 8.1.02419 The ``show_default`` parameter overrides2420 ``Context.show_default``.24212422 .. versionchanged:: 8.1.02423 The default of a single option boolean flag is not shown if the2424 default value is ``False``.24252426 .. versionchanged:: 8.0.1
4 unmodified lines
24312432 def __init__(2433 self,2434 param_decls: t.Optional[t.Sequence[str]] = None,2425 show_default: t.Union[bool, str] = False,2435 show_default: t.Union[bool, str, None] = None,2436 prompt: t.Union[bool, str] = False,2437 confirmation_prompt: t.Union[bool, str] = False,2438 prompt_required: bool = True,2439 hide_input: bool = False,
258 unmodified lines
2698 default_value = self.get_default(ctx, call=False)2699 finally:2700 ctx.resilient_parsing = resilient27012692 show_default_is_str = isinstance(self.show_default, str)2702 show_default = False2703 show_default_is_str = False27042694 if show_default_is_str or (2695 default_value is not None and (self.show_default or ctx.show_default)2696 ):2705 if self.show_default is not None:2706 if isinstance(self.show_default, str):2707 show_default_is_str = show_default = True2708 else:2709 show_default = self.show_default2710 elif ctx.show_default is not None:2711 show_default = ctx.show_default27122713 if show_default_is_str or (show_default and (default_value is not None)):2714 if show_default_is_str:2715 default_string = f"({self.show_default})"2716 elif isinstance(default_value, (list, tuple)):2717 default_string = ", ".join(str(d) for d in default_value)
233 unmodified lines
modified:tests/test_options.py+22 added-1 removed
641 unmodified lines
642 click.echo(testoption)643644 # Both of the commands should have the --help option now.645 for cmd in (cmd1, cmd2):646646 result = runner.invoke(cmd, ["--help"])647 assert "I am a help text" in result.output648 assert "you wont see me" not in result.output649
144 unmodified lines
794 assert message == "values"795796797@pytest.mark.parametrize(798 ("ctx_value", "opt_value", "expect"),799 [800 (None, None, False),801 (None, False, False),802 (None, True, True),803 (False, None, False),804 (False, False, False),805 (False, True, True),806 (True, None, True),807 (True, False, False),808 (True, True, True),809 (False, "one", True),810 ],811)812def test_show_default_precedence(ctx_value, opt_value, expect):813 ctx = click.Context(click.Command("test"), show_default=ctx_value)814 opt = click.Option("-a", default=1, help="value", show_default=opt_value)815 help = opt.get_help_record(ctx)[1]816 assert ("default:" in help) is expect817818819@pytest.mark.parametrize(820 ("args", "expect"),821 [822 (None, (None, None, ())),823 (["--opt"], ("flag", None, ())),
73 unmodified lines
modified:CHANGES.rst+2 added
20 unmodified lines
21- The ``command`` and ``group`` decorators can be applied with or22 without parentheses. :issue:`1359`23- The ``Path`` type can check whether the target is executable.24 :issue:`1961`25- ``Command.show_default`` overrides ``Context.show_default``, instead26 of the other way around. :issue:`1963`272829Version 8.0.430-------------
948 unmodified lines