Skip to content

Commit 8d030ca

Browse files
committed
Update docs with new features
This adds documentation for `complete` nesting, `:hide-header:` and `:post-process:`.
1 parent d9261ad commit 8d030ca

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

docs/usage.rst

+49
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,29 @@ Once enabled, *sphinx-click* enables automatic documentation for
4444
``short``
4545
List sub-commands with short documentation.
4646

47+
``complete``
48+
List sub-commands with short documentation, then also list them with
49+
full documentation.
50+
4751
``none``
4852
Do not list sub-commands.
4953

5054
Defaults to ``short`` unless ``show-nested`` (deprecated) is set.
5155

56+
``:hide-header:``
57+
Omit the title, description and usage example, thus only showing the
58+
subcommands.
59+
5260
``:commands:``
5361
Document only listed commands.
5462

5563
``:show-nested:``
5664
This option is deprecated; use ``nested`` instead.
5765

66+
``:post-process:``
67+
Add a post-processing hook that will run for each command.
68+
See :ref:`post-processing` for more information.
69+
5870
The generated documentation includes anchors for the generated commands,
5971
their options and their environment variables using the `Sphinx standard
6072
domain`_.
@@ -276,6 +288,43 @@ assuming the group or command in ``git.git`` is named ``cli``.
276288
Refer to `issue #2 <https://github.com/click-contrib/sphinx-click/issues/2>`__
277289
for more information.
278290

291+
.. _post-processing:
292+
293+
Post-processing hook
294+
--------------------
295+
You can add a post-processing hook that will run for each command as its
296+
documentation is generated. To do so, use the ``:post-process:`` option,
297+
e.g.:
298+
299+
.. code-block:: rst
300+
301+
.. click:: module:parser
302+
:prog: hello-world
303+
:post-process: my_library.my_module:my_function
304+
305+
The function will get the command object and a list of
306+
``docutils.nodes.Element`` entries that were generated for it, which you
307+
can then modify to your heart's content.
308+
309+
Example:
310+
311+
.. code-block:: python
312+
313+
def mark_super_user_commands(command: click.Command, nodes: List[Element]) -> None:
314+
"""Marks all commands that start with 'su-' as super user commands."""
315+
if nodes and len(nodes) > 0 and len(nodes[0].children) > 0:
316+
command_node = nodes[0]
317+
if command.name.startswith("su-"):
318+
command_title = command_node.children[0]
319+
text_node: docutils.nodes.Text = command_title.children[0]
320+
command_title.replace(text_node, docutils.nodes.Text(text_node.as_text() + " <-- SUPER!"))
321+
322+
**Note**: The function should be specified using the module path and function
323+
name with a colon between them, e.g. ``my_library.my_module:my_function``.
324+
Be sure that this is accessible when generating the documentation (e.g.
325+
add it to the ``PYTHONPATH``).
326+
327+
279328
.. URLs
280329
281330
.. _Sphinx directive: http://www.sphinx-doc.org/en/stable/extdev/markupapi.html

0 commit comments

Comments
 (0)