Skip to content
Myr

Merge pull request #1916 from pallets/default-resilient

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

show help text with invalid default

authored by David Lord · committed by GitHub · parents 1b49159, 985ca16 · Browse files · 4 files+44 added−5 removed
This commit

State

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

Diff stats

4 files+44 added−5 removed

  • tests:+18 added−1 removed
  • other:+26 added−4 removed

4 files+44 added−5 removed

modified:src/click/core.py+24 added-4 removed
2194 unmodified lines
2195 :param ctx: Current context.2196 :param call: If the default is a callable, call it. Disable to2197 return the callable instead.21982199 .. versionchanged:: 8.0.12200 Type casting can fail in resilient parsing mode. Invalid2201 defaults will not prevent showing help text.22022203 .. versionchanged:: 8.02204 Looks at ``ctx.default_map`` first.22052206 .. versionchanged:: 8.0
10 unmodified lines
2217 return value22182219 value = value()22202221 try:2217 return self.type_cast_value(ctx, value)2222 return self.type_cast_value(ctx, value)2223 except BadParameter:2224 if ctx.resilient_parsing:2225 return value22262227 raise22282229 def add_to_parser(self, parser: OptionParser, ctx: Context) -> None:2230 raise NotImplementedError()22312232 def consume_value(
476 unmodified lines
2709 else ", ".join(str(d) for d in envvar)2710 )2711 extra.append(_("env var: {var}").format(var=var_str))27122713 # Temporarily enable resilient parsing to avoid type casting2714 # failing for the default. Might be possible to extend this to2715 # help formatting in general.2716 resilient = ctx.resilient_parsing2717 ctx.resilient_parsing = True27182719 try:2703 default_value = self.get_default(ctx, call=False)2720 default_value = self.get_default(ctx, call=False)2721 finally:2722 ctx.resilient_parsing = resilient27232724 show_default_is_str = isinstance(self.show_default, str)27252726 if show_default_is_str or (2727 default_value is not None and (self.show_default or ctx.show_default)2728 ):2729 if show_default_is_str:2710 default_string: t.Union[str, t.Any] = f"({self.show_default})"2730 default_string = f"({self.show_default})"2731 elif isinstance(default_value, (list, tuple)):2732 default_string = ", ".join(str(d) for d in default_value)2733 elif callable(default_value):2734 default_string = _("(dynamic)")
3 unmodified lines
2738 default_string = split_opt(2739 (self.opts if self.default else self.secondary_opts)[0]2740 )[1]2741 else:2722 default_string = default_value2742 default_string = str(default_value)27432744 extra.append(_("default: {default}").format(default=default_string))27452746 if isinstance(self.type, types._NumberRangeBase):
204 unmodified lines
modified:tests/test_basic.py+17 added
545 unmodified lines
546547 result = runner.invoke(cli, ["--help"])548 assert "Summary line without period" in result.output549 assert "Here is a sentence." not in result.output550551552def test_help_invalid_default(runner):553 cli = click.Command(554 "cli",555 params=[556 click.Option(557 ["-a"],558 type=click.Path(exists=True),559 default="not found",560 show_default=True,561 ),562 ],563 )564 result = runner.invoke(cli, ["--help"])565 assert result.exit_code == 0566 assert "default: not found" in result.output
modified:tests/test_options.py+1 added-1 removed
551 unmodified lines
552553554def test_argument_custom_class(runner):555 class CustomArgument(click.Argument):556 def get_default(self, ctx):556 def get_default(self, ctx, call=True):557 """a dumb override of a default value for testing"""558 return "I am a default"559560 @click.command()
205 unmodified lines
modified:CHANGES.rst+2 added
23 unmodified lines
24 ``command.name``, fixing an unintended change to help text and25 ``default_map`` lookups. When using patterns like ``AliasedGroup``,26 override ``resolve_command`` to change the name that is returned if27 needed. :issue:`1895`28- If a default value is invalid, it does not prevent showing help29 text. :issue:`1889`303132Version 8.0.033-------------
856 unmodified lines