sphinx-autodoc-argparse¶
Modern Sphinx extension for documenting argparse CLIs. The base package
registers the argparse directive plus renderer config values; the
sphinx_autodoc_argparse.exemplar layer adds example extraction, lexers, and CLI
inline roles.
$ pip install sphinx-autodoc-argparse
Working usage examples¶
extensions = [
"sphinx_autodoc_argparse",
"sphinx_autodoc_argparse.exemplar",
]
argparse_examples_section_title = "Examples"
argparse_reorder_usage_before_examples = True
Live demos¶
Base parser rendering¶
Example CLI showing how sphinx-autodoc-argparse renders parsers.
Usage¶
usage: myapp [-h] [--verbose] [--config PATH] {mysubcommand,myothersubcommand} ...
Options¶
Subcommand rendering¶
Drill into a single subcommand with :path::
Execute the primary task with configurable output.
Usage¶
usage: myapp mysubcommand [-h] [--output DIR] [--format {text,json}] [--clean]
Options¶
Inline roles¶
The exemplar layer also registers live inline roles for CLI prose:
myapp, --verbose, json,
DIR, and text.
Cross-reference roles¶
Every .. argparse:: block populates a dedicated argparse domain
alongside the existing std:cmdoption entries. Use these roles to
link to programs, options, subcommands, and positional arguments
declared anywhere in the project:
Role |
Resolves to |
Example |
|---|---|---|
|
A top-level program |
|
|
An optional flag, scoped by program |
|
|
A subcommand under a parent program |
|
|
A positional argument, scoped by program |
|
Whitespace-joined targets (myapp sync --force) are split on the final
space to match the stored (program, name) tuple. Bare forms
(--verbose) also resolve when only one registration matches, though
the fully-qualified form is preferred for multi-program sites.
Auto-generated indices¶
Two domain indices are built into every project that loads the extension:
argparse-programsindex— alphabetised list of every registered program; link via:ref:`argparse-programsindex`.argparse-optionsindex— options grouped by program, alphabetised within each group; link via:ref:`argparse-optionsindex`.
Intersphinx compatibility¶
The classic :option: / std:cmdoption emission is preserved — both
roles resolve and both appear in objects.inv. Downstream consumers
linking via intersphinx continue to work; new authoring inside
projects using this extension can prefer the :argparse:* namespace
for program-scoped clarity.
Configuration values¶
Base extension¶
Config Value Index
Name |
Type |
Default |
Rebuild |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
argparse_group_title_prefix¶
Prefix for argument group titles
- Type:
str- Default:
''- Registered by:
sphinx_autodoc_argparse.setup()
-
argparse_show_defaults¶
Show default values in argument docs
- Type:
bool- Default:
True- Registered by:
sphinx_autodoc_argparse.setup()
-
argparse_show_choices¶
Show choice constraints
- Type:
bool- Default:
True- Registered by:
sphinx_autodoc_argparse.setup()
-
argparse_show_types¶
Show type information
- Type:
bool- Default:
True- Registered by:
sphinx_autodoc_argparse.setup()
Exemplar layer¶
Config Value Index
Name |
Type |
Default |
Rebuild |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
argparse_examples_term_suffix¶
Term suffix for detecting example definition lists
- Type:
str- Default:
'examples'- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_examples_base_term¶
Base term for matching example sections
- Type:
str- Default:
'examples'- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_examples_section_title¶
Section title for extracted examples
- Type:
str- Default:
'Examples'- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_usage_pattern¶
Pattern to detect usage blocks in epilog text
- Type:
str- Default:
'usage:'- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_examples_command_prefix¶
Prefix for example commands in code blocks
- Type:
str- Default:
'$ '- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_examples_code_language¶
Language for example code blocks
- Type:
str- Default:
'console'- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_examples_code_classes¶
CSS classes for example code blocks
- Type:
list- Default:
['highlight-console']- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_usage_code_language¶
Language for usage code blocks
- Type:
str- Default:
'cli-usage'- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
-
argparse_reorder_usage_before_examples¶
Move usage sections before examples sections
- Type:
bool- Default:
True- Registered by:
sphinx_autodoc_argparse.exemplar.setup()
Registered directives and roles¶
Base argparse directive¶
-
.. argparse::
Sphinx directive for documenting argparse-based CLI tools.
Options:
- Python path:
sphinx_autodoc_argparse.directive.ArgparseDirective- Required arguments:
0- Optional arguments:
0- Final argument whitespace:
False- Has content:
True
Exemplar override¶
-
.. cleanargparse::¶
ArgParse directive that strips ANSI codes and formats examples.
Options:
- Python path:
sphinx_autodoc_argparse.exemplar.CleanArgParseDirective- Required arguments:
0- Optional arguments:
0- Final argument whitespace:
False- Has content:
True
CLI role callables¶
Role Index
Name |
Python path |
Summary |
|---|---|---|
|
|
Role for CLI choice values like json or yaml. |
|
|
Role for CLI command names like sync or add. |
|
|
Role for CLI default values like None or “default”. |
|
|
Role for CLI metavar placeholders like FILE or PATH. |
|
|
Role for CLI options like –foo or -h. |
-
:cli-choice:¶
Role for CLI choice values like json or yaml.
- Python path:
sphinx_autodoc_argparse.roles.cli_choice_role
-
:cli-command:¶
Role for CLI command names like sync or add.
- Python path:
sphinx_autodoc_argparse.roles.cli_command_role
-
:cli-default:¶
Role for CLI default values like None or “default”.
- Python path:
sphinx_autodoc_argparse.roles.cli_default_role
-
:cli-metavar:¶
Role for CLI metavar placeholders like FILE or PATH.
- Python path:
sphinx_autodoc_argparse.roles.cli_metavar_role
-
:cli-option:¶
Role for CLI options like –foo or -h.
- Python path:
sphinx_autodoc_argparse.roles.cli_option_role
Downstream usage snippets¶
Use native MyST directives in Markdown:
```{argparse}
:module: myproject.cli
:func: create_parser
:prog: myproject
```
Or reStructuredText:
.. argparse::
:module: myproject.cli
:func: create_parser
:prog: myproject
Copyable config snippet
extensions = [
"sphinx_autodoc_argparse",
"sphinx_autodoc_argparse.exemplar",
]
Package metadata
Source on GitHub: sphinx-autodoc-argparse
PyPI: sphinx-autodoc-argparse
Maturity:
Alpha
Registered Surface
sphinx_autodoc_argparse
Config values
Name |
Default |
Rebuild |
Types |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Directives
Name |
Kind |
Callable |
Summary |
|---|---|---|---|
|
directive |
Sphinx directive for documenting argparse-based CLI tools. |
argparse options
Option |
|
|---|---|
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
sphinx_autodoc_argparse.exemplar
Config values
Name |
Default |
Rebuild |
Types |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Directives
Name |
Kind |
Callable |
Summary |
|---|---|---|---|
|
directive |
ArgParse directive that strips ANSI codes and formats examples. |
argparse options
Option |
|
|---|---|
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
|
Registered option |
Roles
Name |
Kind |
Callable |
Summary |
|---|---|---|---|
|
role |
Role for CLI options like –foo or -h. |
|
|
role |
Role for CLI metavar placeholders like FILE or PATH. |
|
|
role |
Role for CLI command names like sync or add. |
|
|
role |
Role for CLI default values like None or “default”. |
|
|
role |
Role for CLI choice values like json or yaml. |
Lexers
Name |
Callable |
|---|---|
|
|
|
|
|
|
|