Skip to content

Commit f066122

Browse files
committed
Use 'nested_parse_with_titles'
This allows us to embed titles into our docstrings and have them render at the correct level. A test is added, though since this is only testing the formatter it not a great one. We'll have to integrate 'sphinx.testing' in the future. Signed-off-by: Stephen Finucane <stephen@that.guru> Closes: #63
1 parent 8bfb76a commit f066122

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

docs/usage.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ module:
7777
"""Greet the world."""
7878
click.echo('Hello world!')
7979
80-
8180
To document this, use the following:
8281

8382
.. code-block:: rst

sphinx_click/ext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from docutils.parsers import rst
66
from docutils.parsers.rst import directives
77
from sphinx.util import logging
8+
from sphinx.util import nodes as sphinx_nodes
89

910
LOG = logging.getLogger(__name__)
1011
CLICK_VERSION = tuple(int(x) for x in click.__version__.split('.')[0:2])
@@ -389,7 +390,7 @@ def _generate_nodes(
389390
LOG.debug(line)
390391
result.append(line, source_name)
391392

392-
self.state.nested_parse(result, 0, section)
393+
sphinx_nodes.nested_parse_with_titles(self.state, result, section)
393394

394395
# Subcommands
395396

tests/test_formatter.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,52 @@ def foobar():
167167

168168
self.assertEqual('', '\n'.join(output))
169169

170+
def test_titles(self):
171+
"""Validate a `click.Command` with nested titles."""
172+
173+
@click.command()
174+
@click.option('--name', help='Name to say hello to.', required=True, type=str)
175+
def hello(name):
176+
"""Prints hello to name given.
177+
178+
Examples
179+
--------
180+
181+
.. code:: bash
182+
183+
my_cli hello --name "Jack"
184+
"""
185+
186+
ctx = click.Context(hello, info_name='hello')
187+
output = list(ext._format_command(ctx, show_nested=False))
188+
189+
self.assertEqual(
190+
textwrap.dedent(
191+
"""
192+
Prints hello to name given.
193+
194+
Examples
195+
--------
196+
197+
.. code:: bash
198+
199+
my_cli hello --name "Jack"
200+
201+
.. program:: hello
202+
.. code-block:: shell
203+
204+
hello [OPTIONS]
205+
206+
.. rubric:: Options
207+
208+
.. option:: --name <name>
209+
210+
Name to say hello to. [required]
211+
"""
212+
).lstrip(),
213+
'\n'.join(output),
214+
)
215+
170216

171217
class GroupTestCase(unittest.TestCase):
172218
def test_no_parameters(self):

0 commit comments

Comments
 (0)