Merge pull request #1998 from MLH-Fellowship/1971-hide-defaults
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Hide single option boolean flags in "help" when the default is False
authored by David Lord · committed by GitHub · parents 68e65ee, 2cc7244 · Browse files · 5 files·+72 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+72 added−19 removed
- tests:+28 added−6 removed
- other:+44 added−13 removed
Provenance
- Commit
5d759e0- Tree
d1962ee- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
68e65ee- Parent 2
2cc7244
5 files changed+72 added−19 removed
modified:docs/options.rst+21 added
107 unmodified lines108 .. click:run::109 110 invoke(dots, args=['--help'])111 112+For single option boolean flags, the default remains hidden if the default113+value is False.114+115+.. click:example::116+117+ @click.command()118+ @click.option('--n', default=1, show_default=True)119+ @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")120+ @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")121+ def dots(n, gr, br):122+ if gr:123+ click.echo('Hello world!')124+ click.echo('.' * n)125+ if br:126+ click.echo('-' * n)127+128+.. click:run::129+130+ invoke(dots, args=['--help'])131+132+133 Multi Value Options134 -------------------135 136 Sometimes, you have options that take more than one argument. For options,799 unmodified linesmodified:src/click/core.py+20 added-13 removed
2414 unmodified lines2415 have some extra features that arguments don't have.2416 2417 All other parameters are passed onwards to the parameter constructor.2418 2419- :param show_default: controls if the default value should be shown on the2420- help page. Normally, defaults are not shown. If this2421- value is a string, it shows the string instead of the2422- value. This is particularly useful for dynamic options.2423- :param show_envvar: controls if an environment variable should be shown on2424- the help page. Normally, environment variables2425- are not shown.2426- :param prompt: if set to `True` or a non empty string then the user will be2427- prompted for input. If set to `True` the prompt will be the2428- option name capitalized.2419+ :param show_default: Controls if the default value should be shown2420+ on the help page. Normally, defaults are not shown. If this2421+ value is a string, it shows that string in parentheses instead2422+ of the actual value. This is particularly useful for dynamic2423+ options. For single option boolean flags, the default remains2424+ hidden if its value is ``False``.2425+ :param show_envvar: Controls if an environment variable should be2426+ shown on the help page. Normally, environment ariables are not2427+ shown.2428+ :param prompt: If set to ``True`` or a non empty string then the2429+ user will be prompted for input. If set to ``True`` the prompt2430+ will be the option name capitalized.2431 :param confirmation_prompt: Prompt a second time to confirm the2432 value if it was prompted for. Can be set to a string instead of2433 ``True`` to customize the message.2434 :param prompt_required: If set to ``False``, the user will be2435 prompted for input only when the option was specified as a flag2436 without a value.2435- :param hide_input: if this is `True` then the input on the prompt will be2436- hidden from the user. This is useful for password2437- input.2437+ :param hide_input: If this is ``True`` then the input on the prompt2438+ will be hidden from the user. This is useful for password input.2439 :param is_flag: forces this option to act as a flag. The default is2440 auto detection.2441 :param flag_value: which value should be used for this flag if it's2442 enabled. This is set to a boolean automatically if9 unmodified lines2452 context.2453 :param help: the help string.2454 :param hidden: hide this option from help outputs.2455 2456+ .. versionchanged:: 8.1.02457+ The default of a single option boolean flag is not shown if the2458+ default value is ``False``.2459+2460 .. versionchanged:: 8.0.12461 ``type`` is detected from ``flag_value`` if given.2462 """2463 285 unmodified lines2749 # use the opt without prefix instead of the value.2750 default_string = split_opt(2751 (self.opts if self.default else self.secondary_opts)[0]2752 )[1]2753+ elif self.is_bool_flag and not self.secondary_opts and not default_value:2754+ default_string = ""2755 else:2756 default_string = str(default_value)2757 2758 if default_string:207 unmodified linesmodified:tests/test_formatting.py+2 added-1 removed
320 unmodified lines321 def cli():322 pass323 324 result = runner.invoke(cli, ["--help"])325+ # the default to "--help" is not shown because it is False326 assert result.output.splitlines() == [327 "Usage: cli [OPTIONS]",328 "",329 "Options:",330 " -f TEXT Output file name [default: out.txt]",330- " --help Show this message and exit. [default: False]",331+ " --help Show this message and exit.",332 ]333 334 335 def test_formatting_with_options_metavar_empty(runner):13 unmodified linesmodified:tests/test_options.py+26 added-5 removed
698 unmodified lines699 message = opt.get_help_record(ctx)[1]700 assert f"[default: {expect}]" in message701 702 703-def test_show_default_boolean_flag_value(runner):704- """When a boolean flag only has one opt, it will show the default705- value, not the opt name.703+def test_show_true_default_boolean_flag_value(runner):704+ """When a boolean flag only has one opt and its default is True,705+ it will show the default value, not the opt name.706 """707 opt = click.Option(708- ("--cache",), is_flag=True, show_default=True, help="Enable the cache."708+ ("--cache",),709+ is_flag=True,710+ show_default=True,711+ default=True,712+ help="Enable the cache.",713 )714 ctx = click.Context(click.Command("test"))715 message = opt.get_help_record(ctx)[1]712- assert "[default: False]" in message716+ assert "[default: True]" in message717 718 719+@pytest.mark.parametrize("default", [False, None])720+def test_hide_false_default_boolean_flag_value(runner, default):721+ """When a boolean flag only has one opt and its default is False or722+ None, it will not show the default723+ """724+ opt = click.Option(725+ ("--cache",),726+ is_flag=True,727+ show_default=True,728+ default=default,729+ help="Enable the cache.",730+ )731+ ctx = click.Context(click.Command("test"))732+ message = opt.get_help_record(ctx)[1]733+ assert "[default: " not in message734+735+736 def test_show_default_string(runner):737 """When show_default is a string show that value as default."""738 opt = click.Option(["--limit"], show_default="unlimited")739 ctx = click.Context(click.Command("cli"))77 unmodified linesmodified:CHANGES.rst+3 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- Single options boolean flags with ``show_default=True`` only show9+ the default if it is ``True``. :issue:`1971`:10 11+12 Version 8.0.213 -------------14 15 Unreleased901 unmodified lines