Skip to content

Commit b1c1fc8

Browse files
committed
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 f60fd4d commit b1c1fc8

32 files changed

+1213
-336
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repos:
3939
language: system
4040
entry: poetry run mypy
4141
types: [python]
42-
exclude: ^docs/
42+
exclude: ^(docs/|test/roots)
4343
- id: black
4444
name: black
4545
language: system

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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# documentation root, use os.path.abspath to make it absolute, like shown here.
1919

2020

21-
#sys.path.insert(0, os.path.abspath('..'))
21+
sys.path.insert(0, os.path.abspath('..'))
2222

2323
# -- General configuration -----------------------------------------------------
2424

@@ -27,7 +27,14 @@
2727

2828
# Add any Sphinx extension module names here, as strings. They can be extensions
2929
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
30-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', 'sphinxarg.ext']
30+
extensions = [
31+
'sphinx.ext.autodoc',
32+
'sphinx.ext.todo',
33+
'sphinx.ext.coverage',
34+
'sphinx.ext.viewcode',
35+
'sphinx.ext.intersphinx',
36+
'sphinxarg.ext'
37+
]
3138

3239

3340
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
@@ -37,6 +44,9 @@
3744
html_theme = 'sphinx_rtd_theme'
3845
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
3946

47+
intersphinx_mapping = {
48+
'sphinx': ('https://www.sphinx-doc.org/en/master', None)
49+
}
4050

4151
# Add any paths that contain templates here, relative to this directory.
4252
templates_path = ['_templates']
@@ -60,9 +70,9 @@
6070
#
6171
# The short X.Y version.
6272

63-
import pkg_resources # part of setuptools
73+
import sphinxarg.ext
6474

65-
version = pkg_resources.require("sphinx-argparse")[0].version
75+
version = sphinxarg.ext.__version__
6676

6777
# The full version, including alpha/beta/rc tags.
6878
release = version

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`.

0 commit comments

Comments
 (0)