Skip to content

Commit 22f7bfe

Browse files
committed
Moved sphinxarg options from nested dict to direct options + Added full list to docs
1 parent 8c5a4b4 commit 22f7bfe

File tree

3 files changed

+58
-35
lines changed

3 files changed

+58
-35
lines changed

docs/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The following enhancements to the HTML output are described on the [Usage](https
4848

4949
* Optional command index.
5050
* Optional ``:index-groups:`` field to the directive for an command-by-group index.
51-
* A ``full_subcommand_name`` option to print fully-qualified sub-command headings.
51+
* A ``sphinxarg_full_subcommand_name`` option to print fully-qualified sub-command headings.
5252
This option helps when more than one sub-command offers a ``create`` or ``list`` or other
5353
repeated sub-command.
5454
* Each command heading is a domain-specific link target.

docs/usage.rst

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,29 @@ working dir.
7676

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

79+
80+
Config
81+
======
82+
83+
The following ``conf.py`` config options are available, with their default values.
84+
The next sections will describe the options in more detail.
85+
86+
.. code:: py
87+
88+
sphinxarg_full_subcommand_name = False
89+
90+
sphinxarg_build_commands_index = False
91+
sphinxarg_commands_index_in_toctree = False
92+
93+
sphinxarg_build_commands_by_group_index = False
94+
sphinxarg_commands_by_group_index_in_toctree = False
95+
sphinxarg_commands_by_group_index_file_suffix = "by-group"
96+
sphinxarg_commands_by_group_index_title = "Commands by Group"
97+
98+
7999
.. _about-subcommands:
80100

101+
81102
About Sub-Commands
82103
==================
83104

@@ -134,9 +155,7 @@ the option in the ``conf.py`` for your project:
134155

135156
.. code-block:: python
136157
137-
sphinx_argparse_conf = {
138-
"full_subcommand_name": True,
139-
}
158+
sphinxarg_full_subcommand_name = True
140159
141160
142161
Indices
@@ -154,10 +173,8 @@ To enable the simple command index, add the following to the project ``conf.py``
154173

155174
.. code-block:: python
156175
157-
sphinx_argparse_conf = {
158-
"build_commands_index": True,
159-
"commands_index_in_toctree": True,
160-
}
176+
sphinxarg_build_commands_index = True
177+
sphinxarg_commands_index_in_toctree = True
161178
162179
The first option, ``build_commands_index``, instructs the extension to create the index.
163180
For an HTML build, the index is created with the file name ``commands-index.html`` in the output directory.
@@ -176,10 +193,8 @@ To enable the more complex index, add the following to the project ``conf.py`` f
176193

177194
.. code-block:: python
178195
179-
sphinx_argparse_conf = {
180-
"build_commands_by_group_index": True,
181-
"commands_by_group_index_in_toctree": True,
182-
}
196+
sphinxarg_build_commands_by_group_index = True
197+
sphinxarg_commands_by_group_index_in_toctree = True
183198
184199
Add the ``:index-groups:`` option to the ``argparse`` directive in your documentation files.
185200
Specify one or more groups that the command belongs to (comma-separated).
@@ -195,24 +210,23 @@ Specify one or more groups that the command belongs to (comma-separated).
195210
For an HTML build, the index is created with the file name ``commands-by-group.html`` in the output directory.
196211
You can cross reference the index from other files with the ``:ref:`commands-by-group``` role.
197212

198-
Like the simple index, the ``commands_by_group_index_in_toctree`` option enables you to reference the index in ``toctree`` directives.
213+
Like the simple index, the ``sphinxarg_commands_by_group_index_in_toctree`` option enables you to reference the index in ``toctree`` directives.
199214

200215
This index has two more options.
201216

202217
.. code-block:: python
203218
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-
}
219+
sphinxarg_commands_by_group_index_in_toctree = True
220+
sphinxarg_commands_by_group_index_file_suffix = "by-service"
221+
sphinxarg_commands_by_group_index_title = "Commands by Service"
222+
209223
210-
The ``commands_by_group_index_file_suffix`` option overrides the default index name of ``commands-by-group.html``.
224+
The ``sphinxarg_commands_by_group_index_file_suffix`` option overrides the default index name of ``commands-by-group.html``.
211225
The value ``commands-`` is concatenated with the value you specify.
212226
In the preceding sample, the index file name is created as ``commands-by-service.html``.
213227
If you specify this option, the default reference of ``:ref:`commands-by-group``` is overridden with the value that you create.
214228

215-
The ``commands_by_group_index_title`` option overides the default first-level heading for the file.
229+
The ``sphinxarg_commands_by_group_index_title`` option overrides the default first-level heading for the file.
216230
The default heading is "Commands by Group".
217231
The value you specify replaces the default value.
218232

sphinxarg/ext.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,7 @@ def _print_subcommands(self, data, nested_content, markdown_help=False, settings
524524

525525
definitions = map_nested_definitions(nested_content)
526526
items = []
527-
full_subcommand_name_true = (
528-
('full_subcommand_name', True) in self.config.sphinx_argparse_conf.items()
529-
)
527+
full_subcommand_name_true = self.config.sphinxarg_full_subcommand_name
530528
domain = cast(ArgParseDomain, self.env.domains[ArgParseDomain.name])
531529

532530
if 'children' in data:
@@ -859,6 +857,7 @@ def generate(
859857

860858

861859
class CommandsByGroupIndex(Index):
860+
# Defaults (can be overridden through `conf.py`):
862861
name = 'by-group'
863862
localname = 'Commands by Group'
864863

@@ -977,28 +976,27 @@ def _create_temporary_dummy_file(
977976

978977

979978
def configure_ext(app: Sphinx) -> None:
980-
conf = app.config.sphinx_argparse_conf
981979
domain = cast(ArgParseDomain, app.env.domains[ArgParseDomain.name])
982980
build_index = False
983981
build_by_group_index = False
984-
if 'commands_by_group_index_file_suffix' in conf:
985-
build_by_group_index = True
986-
CommandsByGroupIndex.name = conf.get('commands_by_group_index_file_suffix')
987-
if 'commands_by_group_index_title' in conf:
988-
build_by_group_index = True
989-
CommandsByGroupIndex.localname = conf.get('commands_by_group_index_title')
990-
if ('commands_index_in_toctree', True) in conf.items():
982+
983+
CommandsByGroupIndex.name = app.config.sphinxarg_commands_by_group_index_file_suffix
984+
CommandsByGroupIndex.localname = app.config.sphinxarg_commands_by_group_index_title
985+
986+
if app.config.sphinxarg_commands_index_in_toctree:
991987
build_index = True
992988
docname = f'{ArgParseDomain.name}-{CommandsIndex.name}.rst'
993989
_create_temporary_dummy_file(app, domain, docname, CommandsIndex.localname)
994-
if ('commands_by_group_index_in_toctree', True) in conf.items():
990+
991+
if app.config.sphinxarg_commands_by_group_index_in_toctree:
995992
build_by_group_index = True
996993
docname = f'{ArgParseDomain.name}-{CommandsByGroupIndex.name}.rst'
997994
_create_temporary_dummy_file(app, domain, docname, CommandsByGroupIndex.localname)
998995

999-
if build_index or ('build_commands_index', True) in conf.items():
996+
if build_index or app.config.sphinxarg_build_commands_index:
1000997
domain.indices.append(CommandsIndex)
1001-
if build_by_group_index or ('build_commands_by_group_index', True) in conf.items():
998+
999+
if build_by_group_index or app.config.sphinxarg_build_commands_by_group_index:
10021000
domain.indices.append(CommandsByGroupIndex)
10031001

10041002
# Call setup so that :ref:`commands-...` are link targets.
@@ -1009,7 +1007,18 @@ def setup(app: Sphinx):
10091007
app.setup_extension('sphinx.ext.autodoc')
10101008
app.add_domain(ArgParseDomain)
10111009
app.add_directive('argparse', ArgParseDirective)
1012-
app.add_config_value('sphinx_argparse_conf', {}, 'html', types={dict})
1010+
1011+
# Config options must be mentioned in ``usage.rst`` too!
1012+
1013+
app.add_config_value('sphinxarg_full_subcommand_name', False, 'html', bool)
1014+
app.add_config_value('sphinxarg_build_commands_index', False, 'html', bool)
1015+
app.add_config_value('sphinxarg_commands_index_in_toctree', False, 'html', bool)
1016+
1017+
app.add_config_value('sphinxarg_build_commands_by_group_index', False, 'html', bool)
1018+
app.add_config_value('sphinxarg_commands_by_group_index_in_toctree', False, 'html', bool)
1019+
app.add_config_value('sphinxarg_commands_by_group_index_file_suffix', CommandsByGroupIndex.name, 'html', str)
1020+
app.add_config_value('sphinxarg_commands_by_group_index_title', CommandsByGroupIndex.localname, 'html', str)
1021+
10131022
app.connect('builder-inited', configure_ext)
10141023
app.connect('build-finished', _delete_temporary_files)
10151024
return {

0 commit comments

Comments
 (0)