Skip to content
Myr

show_default doesn't show False default for single boolean flag

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

authored by cAtaman · committed by David Lord · parent 68e65ee · 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

5 files+72 added−19 removed

modified:docs/options.rst+21 added
107 unmodified lines
108.. click:run::109110 invoke(dots, args=['--help'])111112For single option boolean flags, the default remains hidden if the default113value is False.114115.. click:example::116117 @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)127128.. click:run::129130 invoke(dots, args=['--help'])131132133Multi Value Options134-------------------135136Sometimes, you have options that take more than one argument. For options,
799 unmodified lines
modified:src/click/core.py+20 added-13 removed
2414 unmodified lines
2415 have some extra features that arguments don't have.24162417 All other parameters are passed onwards to the parameter constructor.24182419 :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 if
9 unmodified lines
2452 context.2453 :param help: the help string.2454 :param hidden: hide this option from help outputs.24552456 .. versionchanged:: 8.1.02457 The default of a single option boolean flag is not shown if the2458 default value is ``False``.24592460 .. versionchanged:: 8.0.12461 ``type`` is detected from ``flag_value`` if given.2462 """2463
285 unmodified lines
2749 # 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)27572758 if default_string:
207 unmodified lines
modified:tests/test_formatting.py+2 added-1 removed
320 unmodified lines
321 def cli():322 pass323324 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 ]333334335def test_formatting_with_options_metavar_empty(runner):
13 unmodified lines
modified:tests/test_options.py+26 added-5 removed
698 unmodified lines
699 message = opt.get_help_record(ctx)[1]700 assert f"[default: {expect}]" in message701702703def 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.703def 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 message717718719@pytest.mark.parametrize("default", [False, None])720def 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 message734735736def 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 lines
modified:CHANGES.rst+3 added
3 unmodified lines
4-------------56Unreleased78- Single options boolean flags with ``show_default=True`` only show9 the default if it is ``True``. :issue:`1971`:101112Version 8.0.213-------------1415Unreleased
901 unmodified lines