Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix behavior of show_default with context_settings #131

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Added support for ``show_default`` as defined in click context settings.
This allows for option defaults to be rendered in the output. Consistent with
click version 8.1.x, show_default is overridden by Command.show_default.

19 changes: 12 additions & 7 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _get_usage(ctx: click.Context) -> str:
return formatter.getvalue().rstrip('\n') # type: ignore


def _get_help_record(opt: click.Option) -> ty.Tuple[str, str]:
def _get_help_record(ctx: click.Context, opt: click.Option) -> ty.Tuple[str, str]:
"""Re-implementation of click.Opt.get_help_record.

The variant of 'get_help_record' found in Click makes uses of slashes to
Expand Down Expand Up @@ -79,12 +79,17 @@ def _write_opts(opts: ty.List[str]) -> str:

extras = []

if isinstance(opt.show_default, str):
if opt.show_default is not None:
show_default = opt.show_default
else:
show_default = ctx.show_default

if isinstance(show_default, str):
# Starting from Click 7.0 show_default can be a string. This is
# mostly useful when the default is not a constant and
# documentation thus needs a manually written string.
extras.append(':default: ``%s``' % opt.show_default)
elif opt.default is not None and opt.show_default:
extras.append(':default: ``%s``' % show_default)
elif opt.default is not None and show_default:
extras.append(
':default: ``%s``'
% (
Expand Down Expand Up @@ -143,9 +148,9 @@ def _format_usage(ctx: click.Context) -> ty.Generator[str, None, None]:
yield ''


def _format_option(opt: click.Option) -> ty.Generator[str, None, None]:
def _format_option(ctx: click.Context, opt: click.Option) -> ty.Generator[str, None, None]:
"""Format the output for a `click.Option`."""
opt_help = _get_help_record(opt)
opt_help = _get_help_record(ctx, opt)

yield '.. option:: {}'.format(opt_help[0])
if opt_help[1]:
Expand Down Expand Up @@ -173,7 +178,7 @@ def _format_options(ctx: click.Context) -> ty.Generator[str, None, None]:
]

for param in params:
for line in _format_option(param):
for line in _format_option(ctx, param):
yield line
yield ''

Expand Down
34 changes: 34 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,40 @@ def foobar(bar):
'\n'.join(output),
)

def test_show_default(self):
"""Validate formatting of show_default via context_settings."""

@click.command(context_settings={"show_default": True})
@click.option('--no-set', default=0)
@click.option('--set-false', default=0, show_default=False)
def foobar():
"""A sample command."""
pass

ctx = click.Context(foobar, info_name='foobar', show_default=True)
output = list(ext._format_command(ctx, nested='short'))
self.assertEqual(
textwrap.dedent(
"""
A sample command.

.. program:: foobar
.. code-block:: shell

foobar [OPTIONS]

.. rubric:: Options

.. option:: --no-set <no_set>

:default: ``0``

.. option:: --set-false <set_false>
"""
).lstrip(),
'\n'.join(output),
)

def test_hidden(self):
"""Validate a `click.Command` with the `hidden` flag."""

Expand Down
Loading