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 rendering of string-valued defaults #137

Merged
merged 1 commit into from
May 15, 2024
Merged
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,9 @@
---
features:
- |
Now when the default value of an option is a string it will be rendered with
quotes around it.
fixes:
- |
If an option has an empty string default, docutils will no longer emit
warnings saying ``CRITICAL: Unexpected section title or transition.``
8 changes: 4 additions & 4 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ def _write_opts(opts: ty.List[str]) -> 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``' % show_default)
elif opt.default is not None and show_default:
extras.append(':default: ``%r``' % ANSI_ESC_SEQ_RE.sub('', show_default))
elif show_default and opt.default is not None:
extras.append(
':default: ``%s``'
% (
', '.join(str(d) for d in opt.default)
', '.join(repr(d) for d in opt.default)
if isinstance(opt.default, (list, tuple))
else opt.default,
else repr(opt.default),
)
)

Expand Down
16 changes: 13 additions & 3 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def test_defaults(self):
'--only-show-default',
show_default="Some default computed at runtime!",
)
@click.option('--string-default', default="abc", show_default=True)
@click.option('--empty-string-default', default="", show_default=True)
def foobar(bar):
"""A sample command."""
pass
Expand All @@ -273,15 +275,23 @@ def foobar(bar):

.. option:: --param <param>

:default: ``Something computed at runtime``
:default: ``'Something computed at runtime'``

.. option:: --group <group>

:default: ``('foo', 'bar')``

.. option:: --only-show-default <only_show_default>

:default: ``Some default computed at runtime!``
:default: ``'Some default computed at runtime!'``

.. option:: --string-default <string_default>

:default: ``'abc'``

.. option:: --empty-string-default <empty_string_default>

:default: ``''``
"""
).lstrip(),
'\n'.join(output),
Expand Down Expand Up @@ -438,7 +448,7 @@ def foobar():

.. option:: --param <param>

:default: ``Something computed at runtime``
:default: ``'Something computed at runtime'``

A sample epilog.
"""
Expand Down
Loading