Skip to content

Commit ef86b06

Browse files
committed
Add support for epilogs
Signed-off-by: Stephen Finucane <stephen@that.guru> Closes: #51
1 parent 382d7e5 commit ef86b06

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

sphinx_click/ext.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ def _format_subcommand(command):
225225
yield _indent(line)
226226

227227

228+
def _format_epilog(ctx):
229+
"""Format the epilog for a given `click.Command`.
230+
231+
We parse this as reStructuredText, allowing users to embed rich
232+
information in their help messages if they so choose.
233+
"""
234+
epilog_string = ctx.command.epilog
235+
if not epilog_string:
236+
return
237+
238+
for line in statemachine.string2lines(
239+
epilog_string, tab_width=4, convert_whitespace=True
240+
):
241+
yield line
242+
yield ''
243+
244+
228245
def _get_lazyload_commands(multicommand):
229246
commands = {}
230247
for command in multicommand.list_commands(multicommand):
@@ -295,6 +312,11 @@ def _format_command(ctx, show_nested, commands=None):
295312
for line in lines:
296313
yield line
297314

315+
# description
316+
317+
for line in _format_epilog(ctx):
318+
yield line
319+
298320
# if we're nesting commands, we need to do this slightly differently
299321
if show_nested:
300322
return

tests/test_formatter.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,39 @@ def foobar(bar):
112112
'\n'.join(output),
113113
)
114114

115+
def test_help_epilog(self):
116+
"""Validate formatting of explicit help and epilog strings."""
117+
118+
@click.command(help='A sample command.', epilog='A sample epilog.')
119+
@click.option('--param', help='A sample option')
120+
def foobar(bar):
121+
pass
122+
123+
ctx = click.Context(foobar, info_name='foobar')
124+
output = list(ext._format_command(ctx, show_nested=False))
125+
126+
self.assertEqual(
127+
textwrap.dedent(
128+
"""
129+
A sample command.
130+
131+
.. program:: foobar
132+
.. code-block:: shell
133+
134+
foobar [OPTIONS]
135+
136+
.. rubric:: Options
137+
138+
.. option:: --param <param>
139+
140+
A sample option
141+
142+
A sample epilog.
143+
"""
144+
).lstrip(),
145+
'\n'.join(output),
146+
)
147+
115148
@unittest.skipIf(
116149
ext.CLICK_VERSION < (7, 0),
117150
'Allowing show_default to be a string was added in Click 7.0',

0 commit comments

Comments
 (0)