Merge pull request #1566 from achimnol/fix/show-default-propagation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fix propagation of show_default option to sub-commands
authored by David Lord · committed by GitHub · parents 0ad4e79, a2b13e9 · Browse files · 3 files·+42 added−18 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+42 added−18 removed
- tests:+14 added−0 removed
- other:+28 added−18 removed
Provenance
- Commit
4ae204e- Tree
118ed15- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
0ad4e79- Parent 2
a2b13e9
3 files changed+42 added−18 removed
modified:src/click/core.py+26 added-18 removed
171 unmodified lines172 173 A context can be used as context manager in which case it will call174 :meth:`close` on teardown.175 176- .. versionadded:: 2.0177- Added the `resilient_parsing`, `help_option_names`,178- `token_normalize_func` parameters.179-180- .. versionadded:: 3.0181- Added the `allow_extra_args` and `allow_interspersed_args`182- parameters.183-184- .. versionadded:: 4.0185- Added the `color`, `ignore_unknown_options`, and186- `max_content_width` parameters.187-188- .. versionadded:: 7.1189- Added the `show_default` parameter.190-176 :param command: the command class for this context.177 :param parent: the parent context.178 :param info_name: the info name for this invocation. Generally this179 is the most descriptive name for the script or46 unmodified lines226 default is autodetection. This is only needed if ANSI227 codes are used in texts that Click prints which is by228 default not the case. This for instance would affect229 help output.245- :param show_default: if True, shows defaults for all options.246- Even if an option is later created with show_default=False,247- this command-level setting overrides it.230+ :param show_default: Show defaults for all options. If not set,231+ defaults to the value from a parent context. Overrides an232+ option's ``show_default`` argument.233+234+ .. versionchanged:: 8.0235+ The ``show_default`` parameter defaults to the value from the236+ parent context.237+238+ .. versionchanged:: 7.1239+ Added the ``show_default`` parameter.240+241+ .. versionchanged:: 4.0242+ Added the ``color``, ``ignore_unknown_options``, and243+ ``max_content_width`` parameters.244+245+ .. versionchanged:: 3.0246+ Added the ``allow_extra_args`` and ``allow_interspersed_args``247+ parameters.248+249+ .. versionchanged:: 2.0250+ Added the ``resilient_parsing``, ``help_option_names``, and251+ ``token_normalize_func`` parameters.252 """253 254 #: The formatter class to create with :meth:`make_formatter`.255 #:145 unmodified lines401 402 #: Controls if styling output is wanted or not.403 self.color = color404 405+ if show_default is None and parent is not None:406+ show_default = parent.show_default407+408+ #: Show option default values when formatting help text.409 self.show_default = show_default410 411 self._close_callbacks = []412 self._depth = 01802 unmodified linesmodified:tests/test_context.py+14 added
265 unmodified lines266 assert not result.exception267 assert result.output == "foocmd\n"268 269 270+def test_propagate_show_default_setting(runner):271+ """A context's ``show_default`` setting defaults to the value from272+ the parent context.273+ """274+ group = click.Group(275+ commands={276+ "sub": click.Command("sub", params=[click.Option(["-a"], default="a")]),277+ },278+ context_settings={"show_default": True},279+ )280+ result = runner.invoke(group, ["sub", "--help"])281+ assert "[default: a]" in result.output282+283+284 def test_exit_not_standalone():285 @click.command()286 @click.pass_context287 def cli(ctx):72 unmodified linesmodified:CHANGES.rst+2 added
73 unmodified lines74 curly braces to indicate that one is required (``{a|b|c}``).75 :issue:`1272`76 - If only a name is passed to ``option()``, Click suggests renaming it77 to ``--name``. :pr:`1355`78+- A context's ``show_default`` parameter defaults to the value from79+ the parent context. :issue:`1565`80 81 82 Version 7.1.283 -------------635 unmodified lines