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:configdocstring.List it in
suppress_warningsto silence the warning, or inclick_extra.sphinx.fail_on_warnings.FAIL_ON_WARNINGS_CONFIGto 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:
ClickDirectiveBase of the directives expanding a Python expression into a MyST reference.
click:treeandclick:configboth 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 mycliinstead of seeding the namespace with a separateclick:source:hide-source:block. Hard errors (notself.error()) so the build fails fast: a partially rendered reference page hides bugs in what the directive was meant to document.- Return type:
- 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 to1, a root rendered ath2under a document title ath1, and inside anh3section to3, a root rendered ath4.The surrounding level is read from
state.memo.section_level, which theMockStateofmyst-parserfills, never below1.
- class click_extra.sphinx.click_reference.TreeDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶
Bases:
ReferenceDirectiveRender 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--helpblock for the root command.One heading +
click:run--helpblock 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 priorclick:source :hide-source:block or inline the import here.Note
Currently MyST-only. Use the directive in a
.mddocument withmyst_parserenabled.- 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-depthcaps the recursion into nestedclick.Groupcommands (default:10).heading-offsetshifts all generated headings down by N levels. When unset, the directive readsstate.memo.section_leveland uses the surrounding section depth so the root nests one level below the enclosing section: inside the document’sh1title this yields1(root ath2); inside anh3section it yields3(root ath4). Override only when the auto-detected depth is wrong for the page layout.anchor-prefixandlabel-prefixoverride the slug and display prefix used for anchors and labels; both default to the CLI’sclick.Command.name.root-labelsets the heading text for the root help block (default:"Help screen").no-tableskips the summary table;no-rootskips the root--helpblock.
- class click_extra.sphinx.click_reference.ConfigDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶
Bases:
ReferenceDirectiveRender 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.Commandwhoseconfig_schemais set (the schema is pulled off itsConfigOption), 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 priorclick: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:sourceblock documents its options without descriptions. Import the schema from a real module instead (seefield_docstrings()).Note
Currently MyST-only. Use the directive in a
.mddocument withmyst_parserenabled.- option_spec: ClassVar[OptionSpec] = {'heading-offset': <function nonnegative_int>, 'no-examples': <function flag>, 'no-table': <function flag>, 'section': <function unchanged>}¶
Recognized directive options.
heading-offsetshifts all generated headings down by N levels; when unset, the surrounding section depth is used (same behavior asclick:tree).sectionoverrides the TOML table header shown in the per-option examples: it defaults totool.{cli-name}when the argument is a CLI (matching how click-extra and its downstream CLIs read their section frompyproject.toml), and to no header at all for a bare schema; an explicitly empty:section:suppresses the header.no-tableskips the summary table;no-examplesskips the TOML example blocks.
- class click_extra.sphinx.click_reference.ClickDomain(env)[source]¶
Bases:
StatelessDomainRegister the directives of the
clicknamespace:click:sourcewhich renders a Click CLI source codeclick:runwhich renders the results of running a Click CLIclick:treewhich walks a Click command tree and renders the full--helpreference for every subcommand, with a summary table on topclick:configwhich documents a CLI’sconfig_schema: a summary table plus one section per option, with types, defaults, and TOML examples
- 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