Skip to content

Commit b511f56

Browse files
committed
Prettier formatting of detailed opt information
Most of what's requested in the linked bug is already handled, albeit not in as pretty manner as possible. The remainder - type and boolean - are hard to represent cleanly and should be described in the option help text. Signed-off-by: Stephen Finucane <stephen@that.guru> Closes: #6
1 parent 747543d commit b511f56

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

sphinx_click/ext.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,44 @@ def _write_opts(opts):
5151
if opt.secondary_opts:
5252
rv.append(_write_opts(opt.secondary_opts))
5353

54-
help = opt.help or ''
55-
extra = []
54+
out = []
55+
if opt.help:
56+
if opt.required:
57+
out.append('**Required** %s' % opt.help)
58+
else:
59+
out.append(opt.help)
60+
else:
61+
if opt.required:
62+
out.append('**Required**')
63+
64+
extras = []
65+
5666
if opt.default is not None and opt.show_default:
5767
if isinstance(opt.show_default, str):
5868
# Starting from Click 7.0 this can be a string as well. This is
5969
# mostly useful when the default is not a constant and
6070
# documentation thus needs a manually written string.
61-
extra.append('default: %s' % opt.show_default)
71+
extras.append(':default: %s' % opt.show_default)
6272
else:
63-
extra.append(
64-
'default: %s'
73+
extras.append(
74+
':default: %s'
6575
% (
6676
', '.join('%s' % d for d in opt.default)
6777
if isinstance(opt.default, (list, tuple))
6878
else opt.default,
6979
)
7080
)
71-
if opt.required:
72-
extra.append('required')
73-
if extra:
74-
help = '%s[%s]' % (help and help + ' ' or '', '; '.join(extra))
81+
7582
if isinstance(opt.type, click.Choice):
76-
help = "%s\n\n:options: %s" % (
77-
help and help + " " or "",
78-
"|".join(opt.type.choices),
79-
)
83+
extras.append(':options: %s' % '|'.join(opt.type.choices))
84+
85+
if extras:
86+
if out:
87+
out.append('')
88+
89+
out.extend(extras)
8090

81-
return ', '.join(rv), help
91+
return ', '.join(rv), '\n'.join(out)
8292

8393

8494
def _format_description(ctx):

tests/test_formatter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ def foobar(bar):
142142
143143
.. option:: --num-param <num_param>
144144
145-
[default: 42]
145+
:default: 42
146146
147147
.. option:: --param <param>
148148
149-
[default: Something computed at runtime]
149+
:default: Something computed at runtime
150150
"""
151151
).lstrip(),
152152
'\n'.join(output),
@@ -208,7 +208,7 @@ def hello(name):
208208
209209
.. option:: --name <name>
210210
211-
Name to say hello to. [required]
211+
**Required** Name to say hello to.
212212
"""
213213
).lstrip(),
214214
'\n'.join(output),

0 commit comments

Comments
 (0)