Skip to content

Commit 7f29172

Browse files
hoodmanestephenfin
authored andcommitted
FIX Format string defaults correctly
Before this patch, string defaults are formatted without quotes around them. However, if the default is a tuple or list of strings, quotes will be included: default="a" ==> :default: ``a`` default=("a",) ==> :default: ``('a')`` This is a slightly annoying inconsistency, and leads to potential for confusion between `default="0"` and `default=0`. Most bothersome is that in the case of an empty string we get: :default: ```` Which makes docutils angry: CRITICAL: Unexpected section title or transition. This fixes the trouble by formatting the repr of the default.
1 parent 34470b6 commit 7f29172

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Now when the default value of an option is a string it will be rendered with
5+
quotes around it.
6+
fixes:
7+
- |
8+
If an option has an empty string default, docutils will no longer emit
9+
warnings saying ``CRITICAL: Unexpected section title or transition.``

sphinx_click/ext.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ def _write_opts(opts: ty.List[str]) -> str:
109109
# Starting from Click 7.0 show_default can be a string. This is
110110
# mostly useful when the default is not a constant and
111111
# documentation thus needs a manually written string.
112-
extras.append(':default: ``%s``' % show_default)
113-
elif opt.default is not None and show_default:
112+
extras.append(':default: ``%r``' % ANSI_ESC_SEQ_RE.sub('', show_default))
113+
elif show_default and opt.default is not None:
114114
extras.append(
115115
':default: ``%s``'
116116
% (
117-
', '.join(str(d) for d in opt.default)
117+
', '.join(repr(d) for d in opt.default)
118118
if isinstance(opt.default, (list, tuple))
119-
else opt.default,
119+
else repr(opt.default),
120120
)
121121
)
122122

tests/test_formatter.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def test_defaults(self):
248248
'--only-show-default',
249249
show_default="Some default computed at runtime!",
250250
)
251+
@click.option('--string-default', default="abc", show_default=True)
252+
@click.option('--empty-string-default', default="", show_default=True)
251253
def foobar(bar):
252254
"""A sample command."""
253255
pass
@@ -273,15 +275,23 @@ def foobar(bar):
273275
274276
.. option:: --param <param>
275277
276-
:default: ``Something computed at runtime``
278+
:default: ``'Something computed at runtime'``
277279
278280
.. option:: --group <group>
279281
280282
:default: ``('foo', 'bar')``
281283
282284
.. option:: --only-show-default <only_show_default>
283285
284-
:default: ``Some default computed at runtime!``
286+
:default: ``'Some default computed at runtime!'``
287+
288+
.. option:: --string-default <string_default>
289+
290+
:default: ``'abc'``
291+
292+
.. option:: --empty-string-default <empty_string_default>
293+
294+
:default: ``''``
285295
"""
286296
).lstrip(),
287297
'\n'.join(output),
@@ -438,7 +448,7 @@ def foobar():
438448
439449
.. option:: --param <param>
440450
441-
:default: ``Something computed at runtime``
451+
:default: ``'Something computed at runtime'``
442452
443453
A sample epilog.
444454
"""

0 commit comments

Comments
 (0)