click_extra.sphinx.click_reference module¶

Reference directives of the click domain: click:tree and click:config.

Each one evaluates a Python expression in the runner namespace that the click:source and click:run blocks of the page share, and expands what it yields into MyST: a summary table, then one section per command or per configuration option.

ClickDomain lives here too, because it registers every click:* directive: this module imports the ones of click_extra.sphinx.click, which cannot import this module back.

click_extra.sphinx.click_reference.CONFIG_DOCSTRING_HEADING_WARNING = 'click_extra.config_docstring_heading'¶

Type and subtype of the warning about a heading in a click:config docstring.

List it in suppress_warnings to silence the warning, or in click_extra.sphinx.fail_on_warnings.FAIL_ON_WARNINGS_CONFIG to fail the build on it.

class click_extra.sphinx.click_reference.ReferenceDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶

Bases: ClickDirective

Base of the directives expanding a Python expression into a MyST reference.

click:tree and click:config both take one required argument, an expression evaluated in the per-document runner namespace, and an optional body of Python run in that namespace first. Both write MyST headings under the section hosting them, so both refuse an rST host.

required_arguments = 1¶

Number of required directive arguments.

optional_arguments = 0¶

The optional argument overrides the default Pygments language to use.

resolve_argument()[source]¶

Run the body, then evaluate the argument in the runner namespace.

The body lets a page inline from mypkg.cli import mycli instead of seeding the namespace with a separate click:source :hide-source: block. Hard errors (not self.error()) so the build fails fast: a partially rendered reference page hides bugs in what the directive was meant to document.

Return type:

tuple[str, Any]

Returns:

the expression as written, and what it evaluated to.

Raises:

RuntimeError – on an rST host, or when the expression cannot be evaluated.

property heading_offset: int[source]¶

Levels the generated headings are shifted down by.

Set by :heading-offset:. Without it, the headings nest one level below the surrounding section, so the document outline stays consistent wherever the directive is placed: at the document’s top level this resolves to 1, a root rendered at h2 under a document title at h1, and inside an h3 section to 3, a root rendered at h4.

The surrounding level is read from state.memo.section_level, which the MockState of myst-parser fills, never below 1.

class click_extra.sphinx.click_reference.TreeDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶

Bases: ReferenceDirective

Render a complete CLI reference for a Click command and all its subcommands.

Walks the Click command tree at build time and emits, in MyST syntax:

  • A GFM summary table linking each command to its section anchor.

  • A heading + click:run --help block for the root command.

  • One heading + click:run --help block per subcommand, nested by depth.

The directive walks the live command tree on every build, so the reference cannot drift from the CLI it documents.

The required argument is a Python expression evaluated in the per-document runner namespace; it must yield a click.Command. The optional directive body is Python preamble exec’d in the same namespace before evaluation, so authors may either import the CLI in a prior click:source :hide-source: block or inline the import here.

Note

Currently MyST-only. Use the directive in a .md document with myst_parser enabled.

option_spec: ClassVar[OptionSpec] = {'anchor-prefix': <function unchanged>, 'heading-offset': <function nonnegative_int>, 'label-prefix': <function unchanged>, 'max-depth': <function positive_int>, 'no-root': <function flag>, 'no-table': <function flag>, 'root-label': <function unchanged>}¶

Recognized directive options.

max-depth caps the recursion into nested click.Group commands (default: 10). heading-offset shifts all generated headings down by N levels. When unset, the directive reads state.memo.section_level and uses the surrounding section depth so the root nests one level below the enclosing section: inside the document’s h1 title this yields 1 (root at h2); inside an h3 section it yields 3 (root at h4). Override only when the auto-detected depth is wrong for the page layout. anchor-prefix and label-prefix override the slug and display prefix used for anchors and labels; both default to the CLI’s click.Command.name. root-label sets the heading text for the root help block (default: "Help screen"). no-table skips the summary table; no-root skips the root --help block.

run()[source]¶

Execute the directive and render its source and results.

Return type:

list[Node]

class click_extra.sphinx.click_reference.ConfigDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶

Bases: ReferenceDirective

Render the configuration reference of a CLI’s config_schema.

Introspects a configuration schema dataclass at build time and expands, in MyST syntax:

  • A GFM summary table linking each option to its section anchor, with its one-line summary and default value.

  • One heading per option, with its docstring, type, default, and a TOML example pinned to the default value.

Option metadata comes from schema_field_infos(): dotted kebab-case keys, type annotations, defaults from a pristine schema instance, and attribute docstrings (which are parsed as the host document’s markup). The directive documents the live schema on every build, so the reference cannot drift from the configuration it describes.

The required argument is a Python expression evaluated in the per-document runner namespace; it must yield either a click.Command whose config_schema is set (the schema is pulled off its ConfigOption), or a schema dataclass directly. The optional directive body is Python preamble exec’d in the same namespace before evaluation, so authors may either import the CLI in a prior click:source :hide-source: block or inline the import here.

Caution

Attribute docstrings are recovered from the schema’s source file, so a schema defined inside an exec’d click:source block documents its options without descriptions. Import the schema from a real module instead (see field_docstrings()).

Note

Currently MyST-only. Use the directive in a .md document with myst_parser enabled.

option_spec: ClassVar[OptionSpec] = {'heading-offset': <function nonnegative_int>, 'no-examples': <function flag>, 'no-table': <function flag>, 'section': <function unchanged>}¶

Recognized directive options.

heading-offset shifts all generated headings down by N levels; when unset, the surrounding section depth is used (same behavior as click:tree). section overrides the TOML table header shown in the per-option examples: it defaults to tool.{cli-name} when the argument is a CLI (matching how click-extra and its downstream CLIs read their section from pyproject.toml), and to no header at all for a bare schema; an explicitly empty :section: suppresses the header. no-table skips the summary table; no-examples skips the TOML example blocks.

run()[source]¶

Execute the directive and render its source and results.

Return type:

list[Node]

class click_extra.sphinx.click_reference.ClickDomain(env)[source]¶

Bases: StatelessDomain

Register the directives of the click namespace:

  • click:source which renders a Click CLI source code

  • click:run which renders the results of running a Click CLI

  • click:tree which walks a Click command tree and renders the full --help reference for every subcommand, with a summary table on top

  • click:config which documents a CLI’s config_schema: a summary table plus one section per option, with types, defaults, and TOML examples

name: ClassVar[str] = 'click'¶

domain name: should be short, but unique

label: ClassVar[str] = 'Click'¶

domain label: longer, more descriptive (used in messages)

directives: ClassVar[dict] = {'config': <class 'click_extra.sphinx.click_reference.ConfigDirective'>, 'run': <class 'click_extra.sphinx.click.RunDirective'>, 'source': <class 'click_extra.sphinx.click.SourceDirective'>, 'tree': <class 'click_extra.sphinx.click_reference.TreeDirective'>}¶

directive name -> directive class