Skip to content

Commit 9ca580b

Browse files
committed
Use a Sphinx Domain
- Print fully-qualified subcommand name in title - Support commands:command role for intersphinx - Prefer fully-qualified HREF targets Use targets like "#blah-sub-commands" as the primary target and move historic targets like "#Sub-commands" to secondary targets. Preserve the older HREF, `sub-commands`, as a secondary target. In the HTML, this becomes a span just below the section element so that bookmarks continue to work even after adopting the update from this commit.
1 parent 2a2a202 commit 9ca580b

30 files changed

+911
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ repos:
3333
language: system
3434
entry: mypy
3535
types: [python]
36-
exclude: ^docs/
36+
exclude: ^(docs|test/roots)/

docs/changelog.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Change log
33
**********
44

5+
<<<<<<< HEAD
56
0.5.2
67
#####
78

@@ -39,6 +40,35 @@ Change log
3940
Patch by Michele Riva in https://github.com/sphinx-doc/sphinx-argparse/pull/53
4041
* Support ``autodoc_mock_imports``.
4142
Patch by Adam Turner and Prajeesh Ag in https://github.com/sphinx-doc/sphinx-argparse/pull/35
43+
=======
44+
0.5.0
45+
#####
46+
47+
The following enhancements to the HTML output are described on the [Usage](https://sphinx-argparse.readthedocs.io/en/latest/usage.html) page.
48+
49+
* Optional command index.
50+
* Optional ``:idxgroups:`` field to the directive for an command-by-group index.
51+
* A ``full_subcommand_name`` option to print fully-qualified sub-command headings.
52+
This option helps when more than one sub-command offers a ``create`` or ``list`` or other
53+
repeated sub-command.
54+
* Each command heading is a domain-specific link target.
55+
You can link to commands and sub-commands with the ``:ref:`` role, but this
56+
release adds support for the domain-specific role like
57+
``:commands:command:`sample-directive-opts A` ``.
58+
The ``:commands:command:`` role supports linking from other projects through the
59+
intersphinx extension.
60+
61+
Changes
62+
63+
* Previously, common headings such as **Positional Arguments** were subject to a
64+
process that made them unique but adding a ``_repeatX`` suffix to the HREF target.
65+
This release continues to support those HREF targets as secondary targets so that
66+
bookmarks continue to work.
67+
However, this release prefers using fully-qualified HREF targets like
68+
``sample-directive-opts-positional-arguments`` as the primary HREF so that customers
69+
are less likely to witness the ``_repeatX`` link in URLs.
70+
71+
>>>>>>> e405138 (Updates for 0.5.0)
4272

4373
0.4.0
4474
#####

docs/usage.rst

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
===========
12
Basic usage
23
===========
34

@@ -75,7 +76,12 @@ working dir.
7576

7677
That's it. Directives will render positional arguments, options and sub-commands.
7778

78-
Sub-commands are limited to one level. But, you can always output help for subcommands separately:
79+
.. _about-subcommands:
80+
81+
About Sub-Commands
82+
==================
83+
84+
Sub-commands are limited to one level. But, you can always output help for subcommands separately::
7985

8086
.. code:: rst
8187
@@ -99,7 +105,7 @@ Nesting level is unlimited:
99105
100106
101107
Other useful directives
102-
-----------------------
108+
=======================
103109

104110
:nodefault: Do not show any default values.
105111

@@ -112,3 +118,122 @@ Other useful directives
112118
:nodescription: Do not parse the description, which can be useful if it contains text that could be incorrectly parse as reStructuredText.
113119

114120
:passparser: This can be used if you don't have a function that returns an argument parser, but rather adds commands to it (``:func:`` is then that function).
121+
122+
:idxgroups: This option is related to grouping related commands in an index.
123+
124+
125+
Printing Fully Qualified Sub-Command Headings
126+
=============================================
127+
128+
By default, when a command has sub-commands, such as ``fancytool install`` shown in the
129+
:ref:`about-subcommands` section, the heading for the sub-command does not include the command name.
130+
For instance, the the heading is **install** rather than **fancytool install**.
131+
132+
If you prefer to show the full command, **fancytool install**, then you can enable
133+
the option in the ``conf.py`` for your project:
134+
135+
.. code-block:: python
136+
137+
sphinx_argparse_conf = {
138+
"full_subcommand_name": True,
139+
}
140+
141+
142+
Indices
143+
=======
144+
145+
The extension supports two types of optional indices.
146+
The first type of index is a simple index that provides a list of all the commands in the project by fully qualified name and a link to each command.
147+
The second type of index enables you to group related commands into groups and then provide a list of the commands and a link to each command.
148+
By default, no index is created.
149+
150+
Simple Command Index
151+
--------------------
152+
153+
To enable the simple command index, add the following to the project ``conf.py`` file:
154+
155+
.. code-block:: python
156+
157+
sphinx_argparse_conf = {
158+
"build_commands_index": True,
159+
"commands_index_in_toctree": True,
160+
}
161+
162+
The first option, ``build_commands_index``, instructs the extension to create the index.
163+
For an HTML build, the index is created with the file name ``commands-index.html`` in the output directory.
164+
You can reference the index from other files with the ``:ref:`commands-index``` markup.
165+
166+
The second option, ``commands_index_in_toctree``, enables you to reference the the index in a ``toctree`` directive.
167+
By default, you cannot reference indices generated by extensions in a ``toctree``.
168+
When you enable this option, the extension creates a temporary file that is named ``commands-index.rst`` in the source directory of your project.
169+
Sphinx locates the temporary file and that makes it possible to reference the file in the ``toctree``.
170+
When the Sphinx build finishes, the extension removes the temporary file from the source directory.
171+
172+
Commands by Group Index
173+
-----------------------
174+
175+
To enable the more complex index, add the following to the project ``conf.py`` file:
176+
177+
.. code-block:: python
178+
179+
sphinx_argparse_conf = {
180+
"build_commands_by_group_index": True,
181+
"commands_by_group_index_in_toctree": True,
182+
}
183+
184+
Add the ``:idxgroups:`` option to the ``argparse`` directive in your documentation files.
185+
Specify one or more groups that the command belongs to.
186+
187+
.. code-block:: reStructuredText
188+
189+
.. argparse::
190+
:filename: ../test/sample.py
191+
:func: parser
192+
:prog: sample
193+
:idxgroups: ["Basic Commands"]
194+
195+
For an HTML build, the index is created with the file name ``commands-by-group.html`` in the output directory.
196+
You can cross reference the index from other files with the ``:ref:`commands-by-group``` role.
197+
198+
Like the simple index, the ``commands_by_group_index_in_toctree`` option enables you to reference the index in ``toctree`` directives.
199+
200+
This index has two more options.
201+
202+
.. code-block:: python
203+
204+
sphinx_argparse_conf = {
205+
"commands_by_group_index_in_toctree": True,
206+
"commands_by_group_index_file_suffix": "by-service",
207+
"commands_by_group_index_title": "Commands by Service",
208+
}
209+
210+
The ``commands_by_group_index_file_suffix`` option overrides the default index name of ``commands-by-group.html``.
211+
The value ``commands-`` is concatenated with the value you specify.
212+
In the preceding sample, the index file name is created as ``commands-by-service.html``.
213+
If you specify this option, the default reference of ``:ref:`commands-by-group``` is overridden with the value that you create.
214+
215+
The ``commands_by_group_index_title`` option overides the default first-level heading for the file.
216+
The default heading is "Commands by Group".
217+
The value you specify replaces the default value.
218+
219+
Customizing the Indices
220+
-----------------------
221+
222+
By default, indices are created with the ``domainindex.html`` template.
223+
If you want to customize the appearance of an index, copy the default ``domainindex.html`` file for your theme to the ``_templates`` directory in your project and modify it.
224+
225+
If you want to customize both indices, but one template cannot accommodate both of them, you can create an additional index template, such as ``customindex.html``.
226+
You can configure Sphinx to use the additional template for an index by modifying the ``conf.py`` for the project like the following example.
227+
228+
.. code-block:: python
229+
230+
def page_template(app: "Sphinx", pagename, templatename, context, doctree) -> str:
231+
if pagename == "commands-by-group":
232+
return "customindex.html"
233+
else:
234+
return templatename
235+
236+
def setup(app: "Sphinx"):
237+
app.connect('html-page-context', page_template)
238+
239+
For more information, refer to the Sphinx documentation for :ref:`sphinx:templating` and the :doc:`sphinx:extdev/appapi`.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ dependencies = [
4141
]
4242
dynamic = ["version"]
4343

44-
4544
[project.optional-dependencies]
4645
markdown = [
4746
"CommonMark>=0.5.6"
@@ -98,6 +97,7 @@ multi_line_output = 3
9897
[tool.mypy]
9998
files = ["sphinxarg", "test"]
10099
python_version = "3.10"
100+
exclude = '''(?x)( ^test/roots | ^docs )'''
101101

102102
[[tool.mypy.overrides]]
103103
module = [

0 commit comments

Comments
 (0)