Skip to content

CLInspector

PyPI License Package status Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors Github Forks Github Issues Github Issues Github Watchers Github Stars Github Repository size Github last commit Github release date Github language count Github commits this month Package status PyUp

Read the documentation!

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

pip install clinspector

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.Typer instances
  • Click: click.Command / click.Group instances
  • Cleo: cleo.application.Application instances
  • Cappa: classes decorated with @cappa.command
  • Cyclopts: cyclopts.App instances
  • argparse: ArgumentParser instances

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, epilog
  • params: list[Param]
  • subcommands: dict[str, CommandInfo] (also reachable via info["name"])
  • deprecated, hidden, callback (the framework callback, not serialized or compared)
  • full_path / full_command: the command path including parents
  • walk(): iterate over the command and all nested subcommands
  • to_dict() / CommandInfo.from_dict(): plain-dict (JSON) round trip
  • build_cli_command(), execute_cli(): run the command as a subprocess

Param (clinspector.Param), among others:

  • name, help, default, opts (e.g. ["-f", "--flag"]), secondary_opts
  • required, is_flag, multiple, nargs, choices, metavar
  • envvar, 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 (markdown extra).
  • 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. Pass handler= (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, tldr extra) and man pages (clinspector.help_providers.man_pages.ManPageProvider, man-pages extra).
  • Help text parser: clinspector.parse_help_text(text) turns raw --help output (argparse, click/typer, rich-click, docopt, cement, cliff, plac, fire layouts) into a CommandInfo. Heuristic: prefer get_cmd_info when you have the CLI object.