show_default uses default_map
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Joachim Jablon · committed by David Lord · parent c067af7 · Browse files · 3 files·+22 added−6 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+22 added−6 removed
- tests:+12 added−0 removed
- other:+10 added−6 removed
Provenance
- Commit
54102a4- Tree
b3b18b0- Author
Joachim Jablon <ewjoachim@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
c067af7
3 files changed+22 added−6 removed
modified:src/click/core.py+7 added-5 removed
2353 unmodified lines2354 if isinstance(envvar, (list, tuple))2355 else envvar2356 )2357 extra.append(f"env var: {var_str}")2358+2359+ default_value = ctx.lookup_default(self.name) or self.default2358- if self.default is not None and (self.show_default or ctx.show_default):2360+ if default_value is not None and (self.show_default or ctx.show_default):2361 if isinstance(self.show_default, str):2362 default_string = f"({self.show_default})"2361- elif isinstance(self.default, (list, tuple)):2362- default_string = ", ".join(str(d) for d in self.default)2363- elif inspect.isfunction(self.default):2363+ elif isinstance(default_value, (list, tuple)):2364+ default_string = ", ".join(str(d) for d in default_value)2365+ elif inspect.isfunction(default_value):2366 default_string = "(dynamic)"2367 elif self.is_bool_flag and self.secondary_opts:2368 # For boolean flags that have distinct True/False opts,2369 # use the opt without prefix instead of the value.2370 default_string = split_opt(2371 (self.opts if self.default else self.secondary_opts)[0]2372 )[1]2373 else:2372- default_string = self.default2374+ default_string = default_value2375 extra.append(f"default: {default_string}")2376 2377 if isinstance(self.type, _NumberRangeBase):2378 range_str = self.type._describe_range()159 unmodified linesmodified:tests/test_options.py+12 added
186 unmodified lines187 assert "foo, bar" in result.output188 assert "1, 2" in result.output189 190 191+def test_show_default_default_map(runner):192+ @click.command()193+ @click.option("--arg", default="a", show_default=True)194+ def cmd(arg):195+ click.echo(arg)196+197+ result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})198+199+ assert not result.exception200+ assert "[default: b]" in result.output201+202+203 def test_multiple_default_type():204 opt = click.Option(["-a"], multiple=True, default=(1, 2))205 assert opt.nargs == 1206 assert opt.multiple454 unmodified linesmodified:CHANGES.rst+3 added-1 removed
132 unmodified lines133 empty tuple. :issue:`472`134 - Handle empty env vars as though the option were not passed. This135 extends the change introduced in 7.1 to be consistent in more cases.136 :issue:`1285`137+- ``show_default`` considers ``default_map`` to display the138+ proper default value. :issue:`1548`139 140 141 Version 7.1.2142 -------------40 unmodified lines183 - Always return one of the passed choices for ``click.Choice``184 :issue:`1277`, :pr:`1318`185 - Add ``no_args_is_help`` option to ``click.Command``, defaults to186 False :pr:`1167`185-- Add ``show_defaults`` parameter to ``Context`` to enable showing187+- Add ``show_default`` parameter to ``Context`` to enable showing188 defaults globally. :issue:`1018`189 - Handle ``env MYPATH=''`` as though the option were not passed.190 :issue:`1196`191 - It is once again possible to call ``next(bar)`` on an active586 unmodified lines