Skip to content

Commit

Permalink
Update docs with new features
Browse files Browse the repository at this point in the history
This adds documentation for `complete` nesting, `:hide-header:` and `:post-process:`.
  • Loading branch information
TomerGodinger authored and stephenfin committed Jan 5, 2025
1 parent 7ae228a commit 4b9ce41
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,29 @@ Once enabled, *sphinx-click* enables automatic documentation for
``short``
List sub-commands with short documentation.

``complete``
List sub-commands with short documentation, then also list them with
full documentation.

``none``
Do not list sub-commands.

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

``:hide-header:``
Omit the title, description and usage example, thus only showing the
subcommands.

``:commands:``
Document only listed commands.

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

``:post-process:``
Add a post-processing hook that will run for each command.
See :ref:`post-processing` for more information.

The generated documentation includes anchors for the generated commands,
their options and their environment variables using the `Sphinx standard
domain`_.
Expand Down Expand Up @@ -276,6 +288,43 @@ assuming the group or command in ``git.git`` is named ``cli``.
Refer to `issue #2 <https://github.com/click-contrib/sphinx-click/issues/2>`__
for more information.

.. _post-processing:

Post-processing hook
--------------------
You can add a post-processing hook that will run for each command as its
documentation is generated. To do so, use the ``:post-process:`` option,
e.g.:

.. code-block:: rst
.. click:: module:parser
:prog: hello-world
:post-process: my_library.my_module:my_function
The function will get the command object and a list of
``docutils.nodes.Element`` entries that were generated for it, which you
can then modify to your heart's content.

Example:

.. code-block:: python
def mark_super_user_commands(command: click.Command, nodes: List[Element]) -> None:
"""Marks all commands that start with 'su-' as super user commands."""
if nodes and len(nodes) > 0 and len(nodes[0].children) > 0:
command_node = nodes[0]
if command.name.startswith("su-"):
command_title = command_node.children[0]
text_node: docutils.nodes.Text = command_title.children[0]
command_title.replace(text_node, docutils.nodes.Text(text_node.as_text() + " <-- SUPER!"))
**Note**: The function should be specified using the module path and function
name with a colon between them, e.g. ``my_library.my_module:my_function``.
Be sure that this is accessible when generating the documentation (e.g.
add it to the ``PYTHONPATH``).


.. URLs
.. _Sphinx directive: http://www.sphinx-doc.org/en/stable/extdev/markupapi.html
Expand Down

0 comments on commit 4b9ce41

Please sign in to comment.