Skip to content
Myr

show boolean flag name for default

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

authored by ovezovs · committed by David Lord · parent 4e0dd2b · Browse files · 3 files+37 added−0 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+37 added−0 removed

  • tests:+29 added−0 removed
  • other:+8 added−0 removed

3 files+37 added−0 removed

modified:src/click/core.py+6 added
1946 unmodified lines
1947 elif isinstance(self.default, (list, tuple)):1948 default_string = ", ".join(str(d) for d in self.default)1949 elif inspect.isfunction(self.default):1950 default_string = "(dynamic)"1951 elif self.is_bool_flag and self.secondary_opts:1952 # For boolean flags that have distinct True/False opts,1953 # use the opt without prefix instead of the value.1954 default_string = split_opt(1955 (self.opts if self.default else self.secondary_opts)[0]1956 )[1]1957 else:1958 default_string = self.default1959 extra.append(f"default: {default_string}")1960
136 unmodified lines
modified:tests/test_options.py+29 added
568 unmodified lines
569570def test_flag_duplicate_names(runner):571 with pytest.raises(ValueError, match="cannot use the same flag for true/false"):572 click.Option(["--foo/--foo"], default=False)573574575@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])576def test_show_default_boolean_flag_name(runner, default, expect):577 """When a boolean flag has distinct True/False opts, it should show578 the default opt name instead of the default value. It should only579 show one name even if multiple are declared.580 """581 opt = click.Option(582 ("--cache/--no-cache", "--c/--nc"),583 default=default,584 show_default=True,585 help="Enable/Disable the cache.",586 )587 ctx = click.Context(click.Command("test"))588 message = opt.get_help_record(ctx)[1]589 assert f"[default: {expect}]" in message590591592def test_show_default_boolean_flag_value(runner):593 """When a boolean flag only has one opt, it will show the default594 value, not the opt name.595 """596 opt = click.Option(597 ("--cache",), is_flag=True, show_default=True, help="Enable the cache.",598 )599 ctx = click.Context(click.Command("test"))600 message = opt.get_help_record(ctx)[1]601 assert "[default: False]" in message
modified:CHANGES.rst+2 added
35 unmodified lines
36- Detect the program name when executing a module or package with37 ``python -m name``. :issue:`1603`38- Include required parent arguments in help synopsis of subcommands.39 :issue:`1475`40- Help for boolean flags with ``show_default=True`` shows the flag41 name instead of ``True`` or ``False``. :issue:`1538`424344Version 7.1.245-------------
635 unmodified lines