Skip to content

Commit e405138

Browse files
mikemckiernanAA-Turner
authored andcommitted
Updates for 0.5.0
- 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 fc253ed commit e405138

31 files changed

+924
-184
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22
Change log
33
**********
44

5+
0.5.0
6+
#####
7+
8+
The following enhancements to the HTML output are described on the [Usage](https://sphinx-argparse.readthedocs.io/en/latest/usage.html) page.
9+
10+
* Optional command index.
11+
* Optional ``:idxgroups:`` field to the directive for an command-by-group index.
12+
* A ``full_subcommand_name`` option to print fully-qualified sub-command headings.
13+
This option helps when more than one sub-command offers a ``create`` or ``list`` or other
14+
repeated sub-command.
15+
* Each command heading is a domain-specific link target.
16+
You can link to commands and sub-commands with the ``:ref:`` role, but this
17+
release adds support for the domain-specific role like
18+
``:commands:command:`sample-directive-opts A` ``.
19+
The ``:commands:command:`` role supports linking from other projects through the
20+
intersphinx extension.
21+
22+
Changes
23+
24+
* Previously, common headings such as **Positional Arguments** were subject to a
25+
process that made them unique but adding a ``_repeatX`` suffix to the HREF target.
26+
This release continues to support those HREF targets as secondary targets so that
27+
bookmarks continue to work.
28+
However, this release prefers using fully-qualified HREF targets like
29+
``sample-directive-opts-positional-arguments`` as the primary HREF so that customers
30+
are less likely to witness the ``_repeatX`` link in URLs.
31+
32+
533
0.4.0
634
#####
735

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
extensions = [
33
'sphinx.ext.autodoc',
44
'sphinx.ext.coverage',
5+
'sphinx.ext.intersphinx',
56
'sphinx.ext.viewcode',
67
'sphinxarg.ext',
78
]

docs/usage.rst

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

@@ -67,6 +68,11 @@ working dir.
6768

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

71+
.. _about-subcommands:
72+
73+
About Sub-Commands
74+
==================
75+
7076
Sub-commands are limited to one level. But, you can always output help for subcommands separately::
7177

7278
.. argparse::
@@ -87,7 +93,7 @@ Nesting level is unlimited::
8793

8894

8995
Other useful directives
90-
-----------------------
96+
=======================
9197

9298
:nodefault: Do not show any default values.
9399

@@ -100,3 +106,122 @@ Other useful directives
100106
:nodescription: Do not parse the description, which can be useful if it contains text that could be incorrectly parse as reStructuredText.
101107

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

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ classifiers = [
3737
]
3838
dependencies = [
3939
"sphinx>=4.0.0",
40+
"CommonMark>=0.9.1",
4041
]
4142
dynamic = ["version"]
4243

43-
4444
[project.optional-dependencies]
4545
markdown = [
4646
"CommonMark>=0.5.6"
@@ -98,6 +98,7 @@ multi_line_output = 3
9898
[tool.mypy]
9999
files = ["sphinxarg", "test"]
100100
python_version = "3.10"
101+
exclude = '''(?x)( ^test/roots | ^docs )'''
101102

102103
[[tool.mypy.overrides]]
103104
module = [

0 commit comments

Comments
 (0)