CLInspector¶
CLInspector introspects Python CLI applications and returns their command tree
(commands, subcommands, options and arguments) as plain, framework-independent
dataclasses. Use it to generate CLI documentation, build completions or wrappers,
or analyze CLI interfaces without parsing --help output.
Installation¶
Framework support is optional; install the extras for the frameworks you use:
pip install "clinspector[click]" # or: typer, cleo, cappa, cyclopts
pip install "clinspector[all]" # all frameworks and optional features
pip install "clinspector[markdown]" # Markdown rendering (jinja2)
pip install "clinspector[tldr]" # tldr page help provider (also: man-pages)
Quickstart¶
import click
from clinspector import get_cmd_info
@click.group()
@click.option("-v", "--verbose", is_flag=True, help="Enable verbose output")
def cli(verbose: bool) -> None:
"""Example CLI tool."""
@cli.command()
@click.argument("target")
@click.option("--release", is_flag=True, help="Build in release mode")
def build(target: str, release: bool) -> None:
"""Build the project."""
info = get_cmd_info(cli)
print(info.name, "-", info.description) # cli - Example CLI tool.
for param in info.params:
print(param.opts, param.help) # ['-v', '--verbose'] Enable verbose output, ...
build_info = info["build"] # subcommands via item access
print(build_info.full_command) # cli build
print([p.name for p in build_info.params]) # ['target', 'release', ...]
Supported frameworks¶
get_cmd_info(instance, command=None) accepts:
- Typer:
typer.Typerinstances - Click:
click.Command/click.Groupinstances - Cleo:
cleo.application.Applicationinstances - Cappa: classes decorated with
@cappa.command - Cyclopts:
cyclopts.Appinstances - argparse:
ArgumentParserinstances
Pass command to get a specific subcommand directly. Nested subcommands use dot
notation ("group.sub"); a leading root command name is ignored ("cli.group.sub").
If no such subcommand exists, the root command is returned. get_cmd_info returns
None for unsupported objects.
Data model¶
CommandInfo (clinspector.CommandInfo):
name,description,usage,epilogparams: list[Param]subcommands: dict[str, CommandInfo](also reachable viainfo["name"])deprecated,hidden,callback(the framework callback, not serialized or compared)full_path/full_command: the command path including parentswalk(): iterate over the command and all nested subcommandsto_dict()/CommandInfo.from_dict(): plain-dict (JSON) round tripbuild_cli_command(),execute_cli(): run the command as a subprocess
Param (clinspector.Param), among others:
name,help,default,opts(e.g.["-f", "--flag"]),secondary_optsrequired,is_flag,multiple,nargs,choices,metavarenvvar,hidden,param_type_name("option"or"argument")type: normalized type name ("str","int","bool","path","choice", ...)
More features¶
- Markdown docs:
clinspector.introspect.markdown.get_cmd_markdown(instance, command=None, include_subcommands=False)renders a command as Markdown (markdownextra). - Synthesis: create a CLI from a
CommandInfo, e.g.clinspector.synthesis.synth_click.create_group,synth_typer.create_app,synth_cleo.create_app,synth_argparse.create_parser,synth_cappa.create_class. Passhandler=(handler(command_info, values)) to run code when a command is invoked (all except argparse, which only parses). - Help providers: async lookups of command examples from tldr pages
(
clinspector.help_providers.tldr_pages.TldrProvider,tldrextra) and man pages (clinspector.help_providers.man_pages.ManPageProvider,man-pagesextra). - Help text parser:
clinspector.parse_help_text(text)turns raw--helpoutput (argparse, click/typer, rich-click, docopt, cement, cliff, plac, fire layouts) into aCommandInfo. Heuristic: preferget_cmd_infowhen you have the CLI object.