Skip to content

Commit e8cf016

Browse files
u-shioristephenfin
andcommitted
Fix show_default with context_settings
Co-authored-by: Stephen Finucane <stephen@that.guru> Closes: #114
1 parent 37742be commit e8cf016

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Added support for ``show_default`` as defined in click context settings.
5+
This allows for option defaults to be rendered in the output. Consistent
6+
with click version 8.1.x, ``show_default`` is overridden by
7+
``Command.show_default``.

sphinx_click/ext.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _get_usage(ctx: click.Context) -> str:
6363
return formatter.getvalue().rstrip('\n') # type: ignore
6464

6565

66-
def _get_help_record(opt: click.Option) -> ty.Tuple[str, str]:
66+
def _get_help_record(ctx: click.Context, opt: click.Option) -> ty.Tuple[str, str]:
6767
"""Re-implementation of click.Opt.get_help_record.
6868
6969
The variant of 'get_help_record' found in Click makes uses of slashes to
@@ -99,12 +99,17 @@ def _write_opts(opts: ty.List[str]) -> str:
9999

100100
extras = []
101101

102-
if isinstance(opt.show_default, str):
102+
if opt.show_default is not None:
103+
show_default = opt.show_default
104+
else:
105+
show_default = ctx.show_default
106+
107+
if isinstance(show_default, str):
103108
# Starting from Click 7.0 show_default can be a string. This is
104109
# mostly useful when the default is not a constant and
105110
# documentation thus needs a manually written string.
106-
extras.append(':default: ``%s``' % opt.show_default)
107-
elif opt.default is not None and opt.show_default:
111+
extras.append(':default: ``%s``' % show_default)
112+
elif opt.default is not None and show_default:
108113
extras.append(
109114
':default: ``%s``'
110115
% (
@@ -165,9 +170,11 @@ def _format_usage(ctx: click.Context) -> ty.Generator[str, None, None]:
165170
yield ''
166171

167172

168-
def _format_option(opt: click.Option) -> ty.Generator[str, None, None]:
173+
def _format_option(
174+
ctx: click.Context, opt: click.Option
175+
) -> ty.Generator[str, None, None]:
169176
"""Format the output for a `click.Option`."""
170-
opt_help = _get_help_record(opt)
177+
opt_help = _get_help_record(ctx, opt)
171178

172179
yield '.. option:: {}'.format(opt_help[0])
173180
if opt_help[1]:
@@ -196,7 +203,7 @@ def _format_options(ctx: click.Context) -> ty.Generator[str, None, None]:
196203
]
197204

198205
for param in params:
199-
for line in _format_option(param):
206+
for line in _format_option(ctx, param):
200207
yield line
201208
yield ''
202209

tests/test_formatter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,40 @@ def foobar(bar):
282282
'\n'.join(output),
283283
)
284284

285+
def test_show_default(self):
286+
"""Validate formatting of show_default via context_settings."""
287+
288+
@click.command(context_settings={"show_default": True})
289+
@click.option('--no-set', default=0)
290+
@click.option('--set-false', default=0, show_default=False)
291+
def foobar():
292+
"""A sample command."""
293+
pass
294+
295+
ctx = click.Context(foobar, info_name='foobar', show_default=True)
296+
output = list(ext._format_command(ctx, nested='short'))
297+
self.assertEqual(
298+
textwrap.dedent(
299+
"""
300+
A sample command.
301+
302+
.. program:: foobar
303+
.. code-block:: shell
304+
305+
foobar [OPTIONS]
306+
307+
.. rubric:: Options
308+
309+
.. option:: --no-set <no_set>
310+
311+
:default: ``0``
312+
313+
.. option:: --set-false <set_false>
314+
"""
315+
).lstrip(),
316+
'\n'.join(output),
317+
)
318+
285319
def test_hidden(self):
286320
"""Validate a `click.Command` with the `hidden` flag."""
287321

0 commit comments

Comments
 (0)