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
Provenance
- Commit
166b312- Tree
f553ded- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
08ecfd0- Parent 2
144f370
3 files changed+56 added−16 removed
modified:src/click/core.py+32 added-15 removed
221 unmodified lines222 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.230 231+ .. versionchanged:: 8.1232+ The ``show_default`` parameter is overridden by233+ ``Command.show_default``, instead of the other way around.234+235 .. versionchanged:: 8.0236 The ``show_default`` parameter defaults to the value from the237 parent context.238 2137 unmodified lines2376 have some extra features that arguments don't have.2377 2378 All other parameters are passed onwards to the parameter constructor.2379 2375- :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 lines2415 :param help: the help string.2416 :param hidden: hide this option from help outputs.2417 2418 .. versionchanged:: 8.1.02419+ The ``show_default`` parameter overrides2420+ ``Context.show_default``.2421+2422+ .. versionchanged:: 8.1.02423 The default of a single option boolean flag is not shown if the2424 default value is ``False``.2425 2426 .. versionchanged:: 8.0.14 unmodified lines2431 2432 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 lines2698 default_value = self.get_default(ctx, call=False)2699 finally:2700 ctx.resilient_parsing = resilient2701 2692- show_default_is_str = isinstance(self.show_default, str)2702+ show_default = False2703+ show_default_is_str = False2704 2694- 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_default2712+2713+ 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 linesmodified:tests/test_options.py+22 added-1 removed
641 unmodified lines642 click.echo(testoption)643 644 # Both of the commands should have the --help option now.645 for cmd in (cmd1, cmd2):646-646 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 lines794 assert message == "values"795 796 797 @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+)812+def 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 expect817+818+819+@pytest.mark.parametrize(820 ("args", "expect"),821 [822 (None, (None, None, ())),823 (["--opt"], ("flag", None, ())),73 unmodified linesmodified:CHANGES.rst+2 added
20 unmodified lines21 - 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`27 28 29 Version 8.0.430 -------------948 unmodified lines