Skip to content

Commit

Permalink
DOC add basic example with CommandCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
tomMoral authored and stephenfin committed Jan 4, 2021
1 parent 9f725e5 commit b6a0508
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = []
extensions = ["sphinx_click"]

# Add any paths that contain templates here, relative to this directory.
templates_path = []
Expand Down
27 changes: 27 additions & 0 deletions docs/examples/commandcollections.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.. _example_commandcollections:

Documentating |CommandCollection|
=================================

The client in the file ``examples/commandcollections/cli.py`` using a
|CommandCollection| such as

.. literalinclude:: ../../examples/commandcollections/cli.py

The automatic documentation using *sphinx-click* gives for the following directive:

.. code-block:: rst
.. click:: commandcollections.cli:cli
:prog: cli
:nested: full
----

.. click:: commandcollections.cli:cli
:prog: cli
:nested: full


.. |CommandCollection| replace:: ``CommandCollection``
.. _CommandCollection: https://click.palletsprojects.com/en/7.x/api/#click.CommandCollection
8 changes: 8 additions & 0 deletions docs/examples/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Examples
========

.. toctree::
:maxdepth: 1
:glob:

*
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __ http://click.pocoo.org/
usage
contributing
changelog
examples/index

.. seealso::

Expand Down
24 changes: 24 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ the roles provided by the `Sphinx standard domain`_.

__ https://github.com/sphinx-doc/sphinx/issues/880

Documenting |CommandCollection|_
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When building more complex CLI, one might need to bring together multiple groups
of commands and make them accessible using a single client with |CommandCollection|_.
*sphinx-click* renders collection of commands with multiple sections, one for each
group listed in the command ``sources``. The group names are used as section titles
and the help string from the description are used as section description.
Thus, a client defined using a |CommandCollection| as ``cli`` will be rendered
using *sphinx-click* and the following directive

.. code-block:: rst
.. click:: cli:cli
:prog: cli
:nested: full
with the subcommands of each group in different sections, one for each group in
``sources``. An example is provided in :ref:`example_commandcollections`.


Modifying ``sys.path``
----------------------

Expand Down Expand Up @@ -184,3 +205,6 @@ 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.

.. |CommandCollection| replace:: :code:`CommandCollection`
.. _CommandCollection: https://click.palletsprojects.com/en/7.x/api/#click.CommandCollection
Empty file.
40 changes: 40 additions & 0 deletions examples/commandcollections/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# file: cli.py
import click


main = click.Group(
name='Principal Commands',
help="Principal commands that are used in ``cli``.\n\n"
"The section name and description are obtained using the name and "
"description of the group passed as sources for |CommandCollection|_."
)


@main.command(help='CMD 1')
def cmd1():
print('call cmd 1')


helpers = click.Group(
name='Helper Commands',
help="Helper commands for ``cli``."
)


@helpers.command()
def cmd2():
"Helper command that has no option."
pass


@helpers.command()
@click.option('--user', type=str)
def cmd3(user):
"Helper command with an option."
pass


cli = click.CommandCollection(
name='cli', sources=[main, helpers],
help='Some general info on ``cli``.'
)
7 changes: 7 additions & 0 deletions examples/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import find_packages, setup

setup(
name='sphinx_click_examples',
packages=find_packages('.'),
install_requires=['click']
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ branch = True

[testenv:docs]
commands =
pip install -e {toxinidir}/examples/
sphinx-build -W -b html -d docs/_build/doctrees docs docs/_build/html

[travis]
Expand Down

0 comments on commit b6a0508

Please sign in to comment.