Skip to content

Commit

Permalink
Display 'show_default' string even when 'default=None'
Browse files Browse the repository at this point in the history
Fixes: #93
  • Loading branch information
ariebovenberg authored Feb 2, 2022
1 parent b9ce52d commit 7b17062
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
27 changes: 13 additions & 14 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,20 @@ def _write_opts(opts):

extras = []

if opt.default is not None and opt.show_default:
if isinstance(opt.show_default, str):
# Starting from Click 7.0 this can be a string as well. 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)
else:
extras.append(
':default: %s'
% (
', '.join(str(d) for d in opt.default)
if isinstance(opt.default, (list, tuple))
else opt.default,
)
if isinstance(opt.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'
% (
', '.join(str(d) for d in opt.default)
if isinstance(opt.default, (list, tuple))
else opt.default,
)
)

if isinstance(opt.type, click.Choice):
extras.append(':options: %s' % ' | '.join(str(x) for x in opt.type.choices))
Expand Down
8 changes: 8 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def test_defaults(self):
multiple=True,
show_default=True,
)
@click.option(
'--only-show-default',
show_default="Some default computed at runtime!",
)
def foobar(bar):
"""A sample command."""
pass
Expand Down Expand Up @@ -207,6 +211,10 @@ def foobar(bar):
.. option:: --group <group>
:default: ('foo', 'bar')
.. option:: --only-show-default <only_show_default>
:default: Some default computed at runtime!
"""
).lstrip(),
'\n'.join(output),
Expand Down

0 comments on commit 7b17062

Please sign in to comment.