sphinx-argparse-neo

Beta GitHub PyPI

Modern Sphinx extension for documenting argparse CLIs. The base package registers the argparse directive plus renderer config values; the sphinx_argparse_neo.exemplar layer adds example extraction, lexers, and CLI inline roles.

$ pip install sphinx-argparse-neo

Downstream conf.py

extensions = [
    "sphinx_argparse_neo",
    "sphinx_argparse_neo.exemplar",
]

argparse_examples_section_title = "Examples"
argparse_reorder_usage_before_examples = True

Live directive demos

Base parser rendering

Example CLI showing how sphinx-argparse-neo renders parsers.

Usage

usage: myapp [-h] [--verbose] [--config PATH]
             {mysubcommand,myothersubcommand} ...

Options

--verbose, -v

Enable verbose output.

Default
False
--config PATH

Path to configuration file.

Default
pyproject.toml

Sub-commands

mysubcommand

Run the primary task.

Execute the primary task with configurable output.

Usage

usage: myapp mysubcommand [-h] [--output DIR] [--format {text,json}] [--clean]

Options

--output, -o DIR

Output directory.

Default
build
--format

Output format.

Default
text
Choices
text, json
--clean

Remove previous output first.

Default
False

myothersubcommand

Run a secondary task.

Execute a secondary task with network options.

Usage

usage: myapp myothersubcommand [-h] [--port PORT] [--host HOST]

Options

--port

Port number.

Default
8000
Type
int
--host

Host to bind to.

Default
localhost

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

--output, -o DIR

Output directory.

Default
build
--format

Output format.

Default
text
Choices
text, json
--clean

Remove previous output first.

Default
False

Inline roles

The exemplar layer also registers live inline roles for CLI prose: myapp, --verbose, json, DIR, and text.

Configuration values

Base extension

Config Value Index

Name

Type

Default

Rebuild

argparse_group_title_prefix

str

''

html

argparse_show_defaults

bool

True

html

argparse_show_choices

bool

True

html

argparse_show_types

bool

True

html

argparse_group_title_prefix
Type:

str

Default:

''

Prefix for argument group titles

Registered by sphinx_argparse_neo.setup().

Rebuild: html.

argparse_show_defaults
Type:

bool

Default:

True

Show default values in argument docs

Registered by sphinx_argparse_neo.setup().

Rebuild: html.

argparse_show_choices
Type:

bool

Default:

True

Show choice constraints

Registered by sphinx_argparse_neo.setup().

Rebuild: html.

argparse_show_types
Type:

bool

Default:

True

Show type information

Registered by sphinx_argparse_neo.setup().

Rebuild: html.

Exemplar layer

Config Value Index

Name

Type

Default

Rebuild

argparse_examples_term_suffix

str

'examples'

html

argparse_examples_base_term

str

'examples'

html

argparse_examples_section_title

str

'Examples'

html

argparse_usage_pattern

str

'usage:'

html

argparse_examples_command_prefix

str

'$ '

html

argparse_examples_code_language

str

'console'

html

argparse_examples_code_classes

list

['highlight-console']

html

argparse_usage_code_language

str

'cli-usage'

html

argparse_reorder_usage_before_examples

bool

True

html

argparse_examples_term_suffix
Type:

str

Default:

'examples'

Term suffix for detecting example definition lists

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_examples_base_term
Type:

str

Default:

'examples'

Base term for matching example sections

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_examples_section_title
Type:

str

Default:

'Examples'

Section title for extracted examples

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_usage_pattern
Type:

str

Default:

'usage:'

Pattern to detect usage blocks in epilog text

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_examples_command_prefix
Type:

str

Default:

'$ '

Prefix for example commands in code blocks

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_examples_code_language
Type:

str

Default:

'console'

Language for example code blocks

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_examples_code_classes
Type:

list

Default:

['highlight-console']

CSS classes for example code blocks

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_usage_code_language
Type:

str

Default:

'cli-usage'

Language for usage code blocks

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

argparse_reorder_usage_before_examples
Type:

bool

Default:

True

Move usage sections before examples sections

Registered by sphinx_argparse_neo.exemplar.setup().

Rebuild: html.

Registered directives and roles

Base argparse directive

.. argparse::

Sphinx directive for documenting argparse-based CLI tools.

Python path: sphinx_argparse_neo.directive.ArgparseDirective

Required arguments: 0

Optional arguments: 0

Final argument whitespace: False

Has content: True

Options:

:func:

Validator: unchanged_required.

:mock-modules:

Validator: unchanged.

:module:

Validator: unchanged_required.

:no-choices:

Validator: flag.

:no-defaults:

Validator: flag.

:no-description:

Validator: flag.

:no-epilog:

Validator: flag.

:no-types:

Validator: flag.

:nodefault:

Validator: flag.

:nodescription:

Validator: flag.

:noepilog:

Validator: flag.

:nosubcommands:

Validator: flag.

:path:

Validator: unchanged.

:prog:

Validator: unchanged.

Exemplar override

.. cleanargparse::

ArgParse directive that strips ANSI codes and formats examples.

Python path: sphinx_argparse_neo.exemplar.CleanArgParseDirective

Required arguments: 0

Optional arguments: 0

Final argument whitespace: False

Has content: True

Options:

:func:

Validator: unchanged_required.

:mock-modules:

Validator: unchanged.

:module:

Validator: unchanged_required.

:no-choices:

Validator: flag.

:no-defaults:

Validator: flag.

:no-description:

Validator: flag.

:no-epilog:

Validator: flag.

:no-types:

Validator: flag.

:nodefault:

Validator: flag.

:nodescription:

Validator: flag.

:noepilog:

Validator: flag.

:nosubcommands:

Validator: flag.

:path:

Validator: unchanged.

:prog:

Validator: unchanged.

CLI role callables

Role Index

Name

Python path

Summary

cli-choice

sphinx_argparse_neo.roles.cli_choice_role

Role for CLI choice values like json or yaml.

cli-command

sphinx_argparse_neo.roles.cli_command_role

Role for CLI command names like sync or add.

cli-default

sphinx_argparse_neo.roles.cli_default_role

Role for CLI default values like None or “default”.

cli-metavar

sphinx_argparse_neo.roles.cli_metavar_role

Role for CLI metavar placeholders like FILE or PATH.

cli-option

sphinx_argparse_neo.roles.cli_option_role

Role for CLI options like –foo or -h.

:cli-choice:

Role for CLI choice values like json or yaml.

Python path: sphinx_argparse_neo.roles.cli_choice_role

Options:

  • class: class_option

:cli-command:

Role for CLI command names like sync or add.

Python path: sphinx_argparse_neo.roles.cli_command_role

Options:

  • class: class_option

:cli-default:

Role for CLI default values like None or “default”.

Python path: sphinx_argparse_neo.roles.cli_default_role

Options:

  • class: class_option

:cli-metavar:

Role for CLI metavar placeholders like FILE or PATH.

Python path: sphinx_argparse_neo.roles.cli_metavar_role

Options:

  • class: class_option

:cli-option:

Role for CLI options like –foo or -h.

Python path: sphinx_argparse_neo.roles.cli_option_role

Options:

  • class: class_option

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_argparse_neo",
    "sphinx_argparse_neo.exemplar",
]

Package metadata

Registered Surface

sphinx_argparse_neo

Config values

Name

Default

Rebuild

Types

argparse_group_title_prefix

''

html

str

argparse_show_defaults

True

html

bool

argparse_show_choices

True

html

bool

argparse_show_types

True

html

bool

Directives

Name

Kind

Callable

Summary

argparse

directive

ArgparseDirective

Sphinx directive for documenting argparse-based CLI tools.

argparse options

Option

:func:

Registered option

:mock-modules:

Registered option

:module:

Registered option

:no-choices:

Registered option

:no-defaults:

Registered option

:no-description:

Registered option

:no-epilog:

Registered option

:no-types:

Registered option

:nodefault:

Registered option

:nodescription:

Registered option

:noepilog:

Registered option

:nosubcommands:

Registered option

:path:

Registered option

:prog:

Registered option

sphinx_argparse_neo.exemplar

Config values

Name

Default

Rebuild

Types

argparse_examples_term_suffix

'examples'

html

str

argparse_examples_base_term

'examples'

html

str

argparse_examples_section_title

'Examples'

html

str

argparse_usage_pattern

'usage:'

html

str

argparse_examples_command_prefix

'$ '

html

str

argparse_examples_code_language

'console'

html

str

argparse_examples_code_classes

['highlight-console']

html

list

argparse_usage_code_language

'cli-usage'

html

str

argparse_reorder_usage_before_examples

True

html

bool

Directives

Name

Kind

Callable

Summary

argparse

directive

CleanArgParseDirective

ArgParse directive that strips ANSI codes and formats examples.

argparse options

Option

:func:

Registered option

:mock-modules:

Registered option

:module:

Registered option

:no-choices:

Registered option

:no-defaults:

Registered option

:no-description:

Registered option

:no-epilog:

Registered option

:no-types:

Registered option

:nodefault:

Registered option

:nodescription:

Registered option

:noepilog:

Registered option

:nosubcommands:

Registered option

:path:

Registered option

:prog:

Registered option

Roles

Name

Kind

Callable

Summary

cli-option

docutils role

cli_option_role

Role for CLI options like –foo or -h.

cli-metavar

docutils role

cli_metavar_role

Role for CLI metavar placeholders like FILE or PATH.

cli-command

docutils role

cli_command_role

Role for CLI command names like sync or add.

cli-default

docutils role

cli_default_role

Role for CLI default values like None or “default”.

cli-choice

docutils role

cli_choice_role

Role for CLI choice values like json or yaml.

Lexers

Name

Callable

cli-usage

CLIUsageLexer

argparse

ArgparseLexer

argparse-usage

ArgparseUsageLexer

argparse-help

ArgparseHelpLexer

Source on GitHub · PyPI