Machine-readable help¶

CLIs are increasingly read by something other than a person: a script wiring two tools together, a package manager, a language model deciding which flag to pass. All of them start from --help, and --help is the worst possible source. It is written for a human at a terminal: it wraps to a width, carries ANSI styling, and expresses structure as layout, so a reader has to recover the option list from column alignment and guess where a description ends.

None of that parsing is necessary. The structure was there before it was formatted, and Click Extra hands it over directly.

click_extra.command_doc extracts a command once, into a CommandDoc, and renders that model several ways. Every command gets a --help-format FORMAT option reaching them, and click-extra wrap does the same for a CLI whose author never heard of Click Extra.

A program wants to know

Ask for

Documented in

What does this command do, and what does it accept?

--help-format json, --help-format markdown

this page

What are the parameters, their types, and where did each value come from?

--params

Parameters

What is the resolved configuration, as a file I can edit and replay?

--export-config FORMAT

Configuration

What subcommands exist?

--help-format json-full, or --tree for a person

Command tree

How do I complete this in a shell?

--help-format carapace

Carapace

from click_extra import Choice, argument, command, echo, option


@command(context_settings={"show_envvar": True})
@argument("city", help="Name of the city to report on.")
@option(
    "--units",
    type=Choice(["celsius", "fahrenheit"]),
    default="celsius",
    help="Temperature scale to display.",
)
def weather(city, units):
    """Report the current temperature for a city."""
    echo(f"{city}: 21 degrees {units}.")

The --help-format option¶

One option carrying a format, rather than a flag per format. A CLI’s option list is the most expensive real estate in its help screen, and every reader pays for it whether or not they will ever export anything: a family of --help-json, --help-markdown and --help-carapace flags would widen the label column of every screen, forever. Here a new format costs a registry entry and nothing on screen.

JSON¶

$ weather --help-format json
{
  "name": "weather",
  "short_help": "Report the current temperature for a city.",
  "version": null,
  "synopsis": "weather [OPTIONS] CITY",
  "description": "Report the current temperature for a city.",
  "arguments": [
    {
      "metavar": "CITY",
      "help": "Name of the city to report on."
    }
  ],
  "option_groups": [
    {
      "title": "Options",
      "help": null,
      "options": [
        {
          "names": [
            "--units"
          ],
          "spec": "--units [celsius|fahrenheit]",
          "metavar": "[celsius|fahrenheit]",
          "choices": [
            "celsius",
            "fahrenheit"
          ],
          "help": "Temperature scale to display.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--help",
            "-h"
          ],
          "spec": "--help / -h",
          "metavar": null,
          "choices": null,
          "help": "Show this message and exit.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Configuration options",
      "help": null,
      "options": [
        {
          "names": [
            "--config"
          ],
          "spec": "--config LOCATION",
          "metavar": "LOCATION",
          "choices": null,
          "help": "Location of the configuration file. Supports local path with glob patterns or remote URL.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--no-config"
          ],
          "spec": "--no-config",
          "metavar": null,
          "choices": null,
          "help": "Ignore all configuration files and only use command line parameters and environment variables.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--validate-config"
          ],
          "spec": "--validate-config LOCATION",
          "metavar": "LOCATION",
          "choices": null,
          "help": "Validate the configuration file and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--export-config"
          ],
          "spec": "--export-config FORMAT",
          "metavar": "FORMAT",
          "choices": [
            "toml",
            "yaml",
            "json",
            "json5",
            "jsonc",
            "hjson",
            "xml",
            "plist"
          ],
          "help": "Export the configuration in the selected format to <stdout>, then exit.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Output options",
      "help": null,
      "options": [
        {
          "names": [
            "--accessible"
          ],
          "spec": "--accessible",
          "metavar": null,
          "choices": null,
          "help": "Accessibility mode: disable colors and render tables in a borderless, screen-reader-friendly format.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--color"
          ],
          "spec": "--color[=auto|always|never]",
          "metavar": "[auto|always|never]",
          "choices": [
            "auto",
            "always",
            "never"
          ],
          "help": "Colorize the output. A bare --color is the same as --color=always.",
          "required": false,
          "optional_value": true
        },
        {
          "names": [
            "--no-color"
          ],
          "spec": "--no-color",
          "metavar": null,
          "choices": null,
          "help": "Disable colorization (alias of --color=never).",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--progress",
            "--no-progress"
          ],
          "spec": "--progress / --no-progress",
          "metavar": null,
          "choices": null,
          "help": "Show progress indicators during long operations. Disabled for non-interactive output (pipes, dumb terminals, CI) and by --accessible.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--theme"
          ],
          "spec": "--theme [auto|dark|dracula|light|manpage|monokai|nord|solarized-dark]",
          "metavar": "[auto|dark|dracula|light|manpage|monokai|nord|solarized-dark]",
          "choices": [
            "auto",
            "dark",
            "dracula",
            "light",
            "manpage",
            "monokai",
            "nord",
            "solarized-dark"
          ],
          "help": "Color theme used for help screens.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--table-format"
          ],
          "spec": "--table-format FORMAT",
          "metavar": "FORMAT",
          "choices": [
            "aligned",
            "asciidoc",
            "colon-grid",
            "csv",
            "csv-excel",
            "csv-excel-tab",
            "csv-unix",
            "double-grid",
            "double-outline",
            "fancy-grid",
            "fancy-outline",
            "github",
            "grid",
            "heavy-grid",
            "heavy-outline",
            "hjson",
            "html",
            "jira",
            "json",
            "json5",
            "jsonc",
            "latex",
            "latex-booktabs",
            "latex-longtable",
            "latex-raw",
            "mediawiki",
            "mixed-grid",
            "mixed-outline",
            "moinmoin",
            "orgtbl",
            "outline",
            "pipe",
            "plain",
            "presto",
            "pretty",
            "psql",
            "rounded-grid",
            "rounded-outline",
            "rst",
            "simple",
            "simple-grid",
            "simple-outline",
            "textile",
            "toml",
            "tsv",
            "unsafehtml",
            "vertical",
            "xml",
            "yaml",
            "youtrack"
          ],
          "help": "Rendering style of tables.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Logging options",
      "help": null,
      "options": [
        {
          "names": [
            "--verbosity"
          ],
          "spec": "--verbosity LEVEL",
          "metavar": "LEVEL",
          "choices": [
            "CRITICAL",
            "ERROR",
            "WARNING",
            "INFO",
            "DEBUG"
          ],
          "help": "Either CRITICAL, ERROR, WARNING, INFO, DEBUG.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--verbose",
            "-v"
          ],
          "spec": "--verbose / -v",
          "metavar": null,
          "choices": null,
          "help": "Increase the default WARNING verbosity by one level for each additional repetition of the option.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--quiet",
            "-q"
          ],
          "spec": "--quiet / -q",
          "metavar": null,
          "choices": null,
          "help": "Decrease the default WARNING verbosity by one level for each additional repetition of the option.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--debug"
          ],
          "spec": "--debug",
          "metavar": null,
          "choices": null,
          "help": "Shorthand for --verbosity DEBUG.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Introspection options",
      "help": null,
      "options": [
        {
          "names": [
            "--time",
            "--no-time"
          ],
          "spec": "--time / --no-time",
          "metavar": null,
          "choices": null,
          "help": "Measure and print elapsed execution time.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--params"
          ],
          "spec": "--params",
          "metavar": null,
          "choices": null,
          "help": "Show all CLI parameters, their provenance, defaults and value, then exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--tree"
          ],
          "spec": "--tree",
          "metavar": null,
          "choices": null,
          "help": "Show the tree of nested subcommands and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--man"
          ],
          "spec": "--man",
          "metavar": null,
          "choices": null,
          "help": "Read the command's manual page and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--help-format"
          ],
          "spec": "--help-format [carapace|json|json-full|man|markdown|markdown-full]",
          "metavar": "[carapace|json|json-full|man|markdown|markdown-full]",
          "choices": [
            "carapace",
            "json",
            "json-full",
            "man",
            "markdown",
            "markdown-full"
          ],
          "help": "Render the command in the given format and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--version"
          ],
          "spec": "--version",
          "metavar": null,
          "choices": null,
          "help": "Show the version and exit.",
          "required": false,
          "optional_value": false
        }
      ]
    }
  ],
  "subcommands": [],
  "examples": [],
  "environment": [
    {
      "variable": "WEATHER_UNITS",
      "help": "Temperature scale to display."
    },
    {
      "variable": "WEATHER__CLICK_DEFAULT_HELP",
      "help": "Show this message and exit."
    },
    {
      "variable": "WEATHER_CONFIG",
      "help": "Location of the configuration file. Supports local path with glob patterns or remote URL."
    },
    {
      "variable": "WEATHER_VALIDATE_CONFIG",
      "help": "Validate the configuration file and exit."
    },
    {
      "variable": "WEATHER_EXPORT_CONFIG",
      "help": "Export the configuration in the selected format to <stdout>, then exit."
    },
    {
      "variable": "WEATHER_ACCESSIBLE",
      "help": "Accessibility mode: disable colors and render tables in a borderless, screen-reader-friendly format."
    },
    {
      "variable": "WEATHER_COLOR",
      "help": "Colorize the output. A bare --color is the same as --color=always."
    },
    {
      "variable": "WEATHER_NO_COLOR",
      "help": "Disable colorization (alias of --color=never)."
    },
    {
      "variable": "WEATHER_PROGRESS",
      "help": "Show progress indicators during long operations. Disabled for non-interactive output (pipes, dumb terminals, CI) and by --accessible."
    },
    {
      "variable": "WEATHER_THEME",
      "help": "Color theme used for help screens."
    },
    {
      "variable": "WEATHER_TABLE_FORMAT",
      "help": "Rendering style of tables."
    },
    {
      "variable": "WEATHER_VERBOSITY",
      "help": "Either CRITICAL, ERROR, WARNING, INFO, DEBUG."
    },
    {
      "variable": "WEATHER_VERBOSE",
      "help": "Increase the default WARNING verbosity by one level for each additional repetition of the option."
    },
    {
      "variable": "WEATHER_QUIET",
      "help": "Decrease the default WARNING verbosity by one level for each additional repetition of the option."
    },
    {
      "variable": "WEATHER_DEBUG",
      "help": "Shorthand for --verbosity DEBUG."
    },
    {
      "variable": "WEATHER_TIME",
      "help": "Measure and print elapsed execution time."
    },
    {
      "variable": "WEATHER_PARAMS",
      "help": "Show all CLI parameters, their provenance, defaults and value, then exit."
    },
    {
      "variable": "WEATHER_TREE",
      "help": "Show the tree of nested subcommands and exit."
    },
    {
      "variable": "WEATHER_MAN",
      "help": "Read the command's manual page and exit."
    },
    {
      "variable": "WEATHER_HELP_FORMAT",
      "help": "Render the command in the given format and exit."
    },
    {
      "variable": "WEATHER_VERSION",
      "help": "Show the version and exit."
    }
  ],
  "files": [
    "~/.config/weather/{*.toml,*.yaml,*.yml,*.json,*.json5,*.jwcc,*.jsonc,*.hjson,*.ini,*.xml,*.plist,*.sqlite,*.sqlite3,*.conf,pyproject.toml}"
  ],
  "exit_status": [
    {
      "code": "0",
      "meaning": "Success."
    },
    {
      "code": "1",
      "meaning": "A runtime error, an aborted prompt (Ctrl-D), or a declined confirmation."
    },
    {
      "code": "2",
      "meaning": "A usage error: unknown option, invalid value, missing operand, or an unparsable configuration file."
    },
    {
      "code": "130",
      "meaning": "Interrupted with Ctrl-C: the process ends by SIGINT, reported as 130."
    }
  ]
}

An option whose type enumerates its values carries them in choices, as a list. Read them from there rather than parsing the metavar: an option is free to replace the [celsius|fahrenheit] rendering with a short placeholder to keep its help screen readable, and --table-format FORMAT does exactly that for its fifty values. choices is null for every other type.

An option whose value is optional (usable bare, with a value attached only when the caller supplies one) reports optional_value: true, and its spec folds in the bracketed [=METAVAR] form instead of a space-separated one. DocOptionItem.attached_value is the property behind that rendering: it strips the metavar’s own brackets first, so a Choice value doesn’t double up.

A group also lists what it dispatches to. Each entry names the subcommand, the other spellings it answers to, and its one-line description:

from click_extra import group

@group(name="observatory")
def observatory():
    """Watch the sky."""

@observatory.command(aliases=["st"])
def station():
    """Manage remote weather stations."""
$ observatory --help-format json
{
  "name": "observatory",
  "short_help": "Watch the sky.",
  "version": null,
  "synopsis": "observatory [OPTIONS] COMMAND [ARGS]...",
  "description": "Watch the sky.",
  "arguments": [],
  "option_groups": [
    {
      "title": "Options",
      "help": null,
      "options": [
        {
          "names": [
            "--help",
            "-h"
          ],
          "spec": "--help / -h",
          "metavar": null,
          "choices": null,
          "help": "Show this message and exit.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Configuration options",
      "help": null,
      "options": [
        {
          "names": [
            "--config"
          ],
          "spec": "--config LOCATION",
          "metavar": "LOCATION",
          "choices": null,
          "help": "Location of the configuration file. Supports local path with glob patterns or remote URL.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--no-config"
          ],
          "spec": "--no-config",
          "metavar": null,
          "choices": null,
          "help": "Ignore all configuration files and only use command line parameters and environment variables.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--validate-config"
          ],
          "spec": "--validate-config LOCATION",
          "metavar": "LOCATION",
          "choices": null,
          "help": "Validate the configuration file and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--export-config"
          ],
          "spec": "--export-config FORMAT",
          "metavar": "FORMAT",
          "choices": [
            "toml",
            "yaml",
            "json",
            "json5",
            "jsonc",
            "hjson",
            "xml",
            "plist"
          ],
          "help": "Export the configuration in the selected format to <stdout>, then exit.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Output options",
      "help": null,
      "options": [
        {
          "names": [
            "--accessible"
          ],
          "spec": "--accessible",
          "metavar": null,
          "choices": null,
          "help": "Accessibility mode: disable colors and render tables in a borderless, screen-reader-friendly format.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--color"
          ],
          "spec": "--color[=auto|always|never]",
          "metavar": "[auto|always|never]",
          "choices": [
            "auto",
            "always",
            "never"
          ],
          "help": "Colorize the output. A bare --color is the same as --color=always.",
          "required": false,
          "optional_value": true
        },
        {
          "names": [
            "--no-color"
          ],
          "spec": "--no-color",
          "metavar": null,
          "choices": null,
          "help": "Disable colorization (alias of --color=never).",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--progress",
            "--no-progress"
          ],
          "spec": "--progress / --no-progress",
          "metavar": null,
          "choices": null,
          "help": "Show progress indicators during long operations. Disabled for non-interactive output (pipes, dumb terminals, CI) and by --accessible.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--theme"
          ],
          "spec": "--theme [auto|dark|dracula|light|manpage|monokai|nord|solarized-dark]",
          "metavar": "[auto|dark|dracula|light|manpage|monokai|nord|solarized-dark]",
          "choices": [
            "auto",
            "dark",
            "dracula",
            "light",
            "manpage",
            "monokai",
            "nord",
            "solarized-dark"
          ],
          "help": "Color theme used for help screens.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--table-format"
          ],
          "spec": "--table-format FORMAT",
          "metavar": "FORMAT",
          "choices": [
            "aligned",
            "asciidoc",
            "colon-grid",
            "csv",
            "csv-excel",
            "csv-excel-tab",
            "csv-unix",
            "double-grid",
            "double-outline",
            "fancy-grid",
            "fancy-outline",
            "github",
            "grid",
            "heavy-grid",
            "heavy-outline",
            "hjson",
            "html",
            "jira",
            "json",
            "json5",
            "jsonc",
            "latex",
            "latex-booktabs",
            "latex-longtable",
            "latex-raw",
            "mediawiki",
            "mixed-grid",
            "mixed-outline",
            "moinmoin",
            "orgtbl",
            "outline",
            "pipe",
            "plain",
            "presto",
            "pretty",
            "psql",
            "rounded-grid",
            "rounded-outline",
            "rst",
            "simple",
            "simple-grid",
            "simple-outline",
            "textile",
            "toml",
            "tsv",
            "unsafehtml",
            "vertical",
            "xml",
            "yaml",
            "youtrack"
          ],
          "help": "Rendering style of tables.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Logging options",
      "help": null,
      "options": [
        {
          "names": [
            "--verbosity"
          ],
          "spec": "--verbosity LEVEL",
          "metavar": "LEVEL",
          "choices": [
            "CRITICAL",
            "ERROR",
            "WARNING",
            "INFO",
            "DEBUG"
          ],
          "help": "Either CRITICAL, ERROR, WARNING, INFO, DEBUG.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--verbose",
            "-v"
          ],
          "spec": "--verbose / -v",
          "metavar": null,
          "choices": null,
          "help": "Increase the default WARNING verbosity by one level for each additional repetition of the option.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--quiet",
            "-q"
          ],
          "spec": "--quiet / -q",
          "metavar": null,
          "choices": null,
          "help": "Decrease the default WARNING verbosity by one level for each additional repetition of the option.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--debug"
          ],
          "spec": "--debug",
          "metavar": null,
          "choices": null,
          "help": "Shorthand for --verbosity DEBUG.",
          "required": false,
          "optional_value": false
        }
      ]
    },
    {
      "title": "Introspection options",
      "help": null,
      "options": [
        {
          "names": [
            "--time",
            "--no-time"
          ],
          "spec": "--time / --no-time",
          "metavar": null,
          "choices": null,
          "help": "Measure and print elapsed execution time.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--params"
          ],
          "spec": "--params",
          "metavar": null,
          "choices": null,
          "help": "Show all CLI parameters, their provenance, defaults and value, then exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--tree"
          ],
          "spec": "--tree",
          "metavar": null,
          "choices": null,
          "help": "Show the tree of nested subcommands and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--man"
          ],
          "spec": "--man",
          "metavar": null,
          "choices": null,
          "help": "Read the command's manual page and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--help-format"
          ],
          "spec": "--help-format [carapace|json|json-full|man|markdown|markdown-full]",
          "metavar": "[carapace|json|json-full|man|markdown|markdown-full]",
          "choices": [
            "carapace",
            "json",
            "json-full",
            "man",
            "markdown",
            "markdown-full"
          ],
          "help": "Render the command in the given format and exit.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--version"
          ],
          "spec": "--version",
          "metavar": null,
          "choices": null,
          "help": "Show the version and exit.",
          "required": false,
          "optional_value": false
        }
      ]
    }
  ],
  "subcommands": [
    {
      "name": "help",
      "aliases": [],
      "short_help": "Show help for a command."
    },
    {
      "name": "station",
      "aliases": [
        "st"
      ],
      "short_help": "Manage remote weather stations."
    }
  ],
  "examples": [],
  "environment": [
    {
      "variable": "OBSERVATORY__CLICK_DEFAULT_HELP",
      "help": "Show this message and exit."
    },
    {
      "variable": "OBSERVATORY_CONFIG",
      "help": "Location of the configuration file. Supports local path with glob patterns or remote URL."
    },
    {
      "variable": "OBSERVATORY_VALIDATE_CONFIG",
      "help": "Validate the configuration file and exit."
    },
    {
      "variable": "OBSERVATORY_EXPORT_CONFIG",
      "help": "Export the configuration in the selected format to <stdout>, then exit."
    },
    {
      "variable": "OBSERVATORY_ACCESSIBLE",
      "help": "Accessibility mode: disable colors and render tables in a borderless, screen-reader-friendly format."
    },
    {
      "variable": "OBSERVATORY_COLOR",
      "help": "Colorize the output. A bare --color is the same as --color=always."
    },
    {
      "variable": "OBSERVATORY_NO_COLOR",
      "help": "Disable colorization (alias of --color=never)."
    },
    {
      "variable": "OBSERVATORY_PROGRESS",
      "help": "Show progress indicators during long operations. Disabled for non-interactive output (pipes, dumb terminals, CI) and by --accessible."
    },
    {
      "variable": "OBSERVATORY_THEME",
      "help": "Color theme used for help screens."
    },
    {
      "variable": "OBSERVATORY_TABLE_FORMAT",
      "help": "Rendering style of tables."
    },
    {
      "variable": "OBSERVATORY_VERBOSITY",
      "help": "Either CRITICAL, ERROR, WARNING, INFO, DEBUG."
    },
    {
      "variable": "OBSERVATORY_VERBOSE",
      "help": "Increase the default WARNING verbosity by one level for each additional repetition of the option."
    },
    {
      "variable": "OBSERVATORY_QUIET",
      "help": "Decrease the default WARNING verbosity by one level for each additional repetition of the option."
    },
    {
      "variable": "OBSERVATORY_DEBUG",
      "help": "Shorthand for --verbosity DEBUG."
    },
    {
      "variable": "OBSERVATORY_TIME",
      "help": "Measure and print elapsed execution time."
    },
    {
      "variable": "OBSERVATORY_PARAMS",
      "help": "Show all CLI parameters, their provenance, defaults and value, then exit."
    },
    {
      "variable": "OBSERVATORY_TREE",
      "help": "Show the tree of nested subcommands and exit."
    },
    {
      "variable": "OBSERVATORY_MAN",
      "help": "Read the command's manual page and exit."
    },
    {
      "variable": "OBSERVATORY_HELP_FORMAT",
      "help": "Render the command in the given format and exit."
    },
    {
      "variable": "OBSERVATORY_VERSION",
      "help": "Show the version and exit."
    }
  ],
  "files": [
    "~/.config/observatory/{*.toml,*.yaml,*.yml,*.json,*.json5,*.jwcc,*.jsonc,*.hjson,*.ini,*.xml,*.plist,*.sqlite,*.sqlite3,*.conf,pyproject.toml}"
  ],
  "exit_status": [
    {
      "code": "0",
      "meaning": "Success."
    },
    {
      "code": "1",
      "meaning": "A runtime error, an aborted prompt (Ctrl-D), or a declined confirmation."
    },
    {
      "code": "2",
      "meaning": "A usage error: unknown option, invalid value, missing operand, or an unparsable configuration file."
    },
    {
      "code": "130",
      "meaning": "Interrupted with Ctrl-C: the process ends by SIGINT, reported as 130."
    }
  ]
}

An alias is invocable, so it is reported wherever the command is: aliases is an empty list for a subcommand that declares none.

Markdown¶

The same command, in what a language model reads most comfortably:

$ weather --help-format markdown
# weather

Report the current temperature for a city.

## Synopsis

```shell-session
$ weather [OPTIONS] CITY
```

## Description

Report the current temperature for a city.

## Arguments

- `CITY`: Name of the city to report on.

## Options

### Options

- `--units [celsius|fahrenheit]`: Temperature scale to display.
- `--help / -h`: Show this message and exit.

### Configuration options

- `--config LOCATION`: Location of the configuration file. Supports local path with glob patterns or remote URL.
- `--no-config`: Ignore all configuration files and only use command line parameters and environment variables.
- `--validate-config LOCATION`: Validate the configuration file and exit.
- `--export-config FORMAT`: Export the configuration in the selected format to &lt;stdout&gt;, then exit. Values: `toml`, `yaml`, `json`, `json5`, `jsonc`, `hjson`, `xml`, `plist`.

### Output options

- `--accessible`: Accessibility mode: disable colors and render tables in a borderless, screen-reader-friendly format.
- `--color[=auto|always|never]`: Colorize the output. A bare --color is the same as --color=always.
- `--no-color`: Disable colorization (alias of --color=never).
- `--progress / --no-progress`: Show progress indicators during long operations. Disabled for non-interactive output (pipes, dumb terminals, CI) and by --accessible.
- `--theme [auto|dark|dracula|light|manpage|monokai|nord|solarized-dark]`: Color theme used for help screens.
- `--table-format FORMAT`: Rendering style of tables. Values: `aligned`, `asciidoc`, `colon-grid`, `csv`, `csv-excel`, `csv-excel-tab`, `csv-unix`, `double-grid`, `double-outline`, `fancy-grid`, `fancy-outline`, `github`, `grid`, `heavy-grid`, `heavy-outline`, `hjson`, `html`, `jira`, `json`, `json5`, `jsonc`, `latex`, `latex-booktabs`, `latex-longtable`, `latex-raw`, `mediawiki`, `mixed-grid`, `mixed-outline`, `moinmoin`, `orgtbl`, `outline`, `pipe`, `plain`, `presto`, `pretty`, `psql`, `rounded-grid`, `rounded-outline`, `rst`, `simple`, `simple-grid`, `simple-outline`, `textile`, `toml`, `tsv`, `unsafehtml`, `vertical`, `xml`, `yaml`, `youtrack`.

### Logging options

- `--verbosity LEVEL`: Either CRITICAL, ERROR, WARNING, INFO, DEBUG. Values: `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`.
- `--verbose / -v`: Increase the default WARNING verbosity by one level for each additional repetition of the option.
- `--quiet / -q`: Decrease the default WARNING verbosity by one level for each additional repetition of the option.
- `--debug`: Shorthand for --verbosity DEBUG.

### Introspection options

- `--time / --no-time`: Measure and print elapsed execution time.
- `--params`: Show all CLI parameters, their provenance, defaults and value, then exit.
- `--tree`: Show the tree of nested subcommands and exit.
- `--man`: Read the command's manual page and exit.
- `--help-format [carapace|json|json-full|man|markdown|markdown-full]`: Render the command in the given format and exit.
- `--version`: Show the version and exit.

## Environment variables

- `WEATHER_UNITS`: Temperature scale to display.
- `WEATHER__CLICK_DEFAULT_HELP`: Show this message and exit.
- `WEATHER_CONFIG`: Location of the configuration file. Supports local path with glob patterns or remote URL.
- `WEATHER_VALIDATE_CONFIG`: Validate the configuration file and exit.
- `WEATHER_EXPORT_CONFIG`: Export the configuration in the selected format to &lt;stdout&gt;, then exit.
- `WEATHER_ACCESSIBLE`: Accessibility mode: disable colors and render tables in a borderless, screen-reader-friendly format.
- `WEATHER_COLOR`: Colorize the output. A bare --color is the same as --color=always.
- `WEATHER_NO_COLOR`: Disable colorization (alias of --color=never).
- `WEATHER_PROGRESS`: Show progress indicators during long operations. Disabled for non-interactive output (pipes, dumb terminals, CI) and by --accessible.
- `WEATHER_THEME`: Color theme used for help screens.
- `WEATHER_TABLE_FORMAT`: Rendering style of tables.
- `WEATHER_VERBOSITY`: Either CRITICAL, ERROR, WARNING, INFO, DEBUG.
- `WEATHER_VERBOSE`: Increase the default WARNING verbosity by one level for each additional repetition of the option.
- `WEATHER_QUIET`: Decrease the default WARNING verbosity by one level for each additional repetition of the option.
- `WEATHER_DEBUG`: Shorthand for --verbosity DEBUG.
- `WEATHER_TIME`: Measure and print elapsed execution time.
- `WEATHER_PARAMS`: Show all CLI parameters, their provenance, defaults and value, then exit.
- `WEATHER_TREE`: Show the tree of nested subcommands and exit.
- `WEATHER_MAN`: Read the command's manual page and exit.
- `WEATHER_HELP_FORMAT`: Render the command in the given format and exit.
- `WEATHER_VERSION`: Show the version and exit.

## Files

- `~/.config/weather/{*.toml,*.yaml,*.yml,*.json,*.json5,*.jwcc,*.jsonc,*.hjson,*.ini,*.xml,*.plist,*.sqlite,*.sqlite3,*.conf,pyproject.toml}`

## Exit status

- `0`: Success.
- `1`: A runtime error, an aborted prompt (Ctrl-D), or a declined confirmation.
- `2`: A usage error: unknown option, invalid value, missing operand, or an unparsable configuration file.
- `130`: Interrupted with Ctrl-C: the process ends by SIGINT, reported as 130.

The format list¶

Format

What it renders

carapace

A Carapace completion spec (YAML), which doubles as a command-and-flag tree.

json

This command as a JSON object, its direct subcommands listed by name and aliases.

json-full

Every command of the tree, under a commands array.

man

This command as a man page: the roff source --man typesets to read.

markdown

This command as a Markdown document, one section per topic.

markdown-full

Every command of the tree as one Markdown document.

Each row is a HelpFormat member, and --help-format takes its choices from that enum. HELP_FORMATS maps each member to a one-line description. render_help() renders a command in a given format from Python. The option calls that same function at runtime.

Note

The plain and -full variants differ in how much they hand over at once. A plain render describes one command and names its children, so a reader descends one level at a time rather than pulling a whole tree into a context window to answer a question about one leaf. The -full variants are for the opposite job: generating documentation, or diffing a CLI’s whole surface between two releases.

Whatever --color says, these renderings carry no ANSI codes: they are meant to be piped into a parser, which has no use for escape sequences. --help remains the colorized human view.

Installing the artifacts¶

Two of these renderings are installed rather than read: a man page under a man directory, a Carapace spec under Carapace’s. For those, the wrapper takes a destination instead of printing to stdout:

$ click-extra wrap --help-format man --install -- flask
/home/me/.local/share/man/man1/flask.1
/home/me/.local/share/man/man1/flask-run.1
$ click-extra wrap --help-format carapace --install -- flask
/home/me/.config/carapace/specs/flask.yaml

--install means the same thing for both: put this where its consumer looks for it, honoring XDG_DATA_HOME and XDG_CONFIG_HOME. --output-dir DIR writes it somewhere else instead. Both are refused for the other formats, which are documents nothing goes looking for: a shell redirection is the whole story there.

Any Click CLI¶

A CLI that has never heard of Click Extra gets the same treatment through the wrapper: it is loaded and walked from the outside, so an agent is not limited to the tools whose authors thought about it.

$ click-extra wrap --help-format json -- flask run
$ click-extra wrap --help-format markdown -- flask

The parameter inventory comes out the same way, in any structured format, values keeping their native types:

$ click-extra wrap --params --table-format json -- flask run
[
  {
    "ID": "run._click_default_help",
    "Spec.": "--help",
    "Class": "click.core.Option",
    "Param type": "click.types.BoolParamType",
    "Python type": "bool",
    "Hidden": false,
    "Exposed": false,
    "Env. vars.": [
      "FLASK_RUN__CLICK_DEFAULT_HELP"
    ],
    "Default": false,
    "Is flag": true,
    "Flag value": true,
    "Is bool flag": true,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.cert",
    "Spec.": "--cert PATH",
    "Class": "click.core.Option",
    "Param type": "flask.cli.CertParamType",
    "Python type": "str",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_CERT"
    ],
    "Default": null,
    "Is flag": false,
    "Flag value": null,
    "Is bool flag": false,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.debug",
    "Spec.": "--debug / --no-debug",
    "Class": "click.core.Option",
    "Param type": "click.types.BoolParamType",
    "Python type": "bool",
    "Hidden": false,
    "Exposed": false,
    "Env. vars.": [
      "FLASK_RUN_DEBUG"
    ],
    "Default": false,
    "Is flag": true,
    "Flag value": true,
    "Is bool flag": true,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.debugger",
    "Spec.": "--debugger / --no-debugger",
    "Class": "click.core.Option",
    "Param type": "click.types.BoolParamType",
    "Python type": "bool",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_DEBUGGER"
    ],
    "Default": null,
    "Is flag": true,
    "Flag value": true,
    "Is bool flag": true,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.exclude_patterns",
    "Spec.": "--exclude-patterns PATH",
    "Class": "click.core.Option",
    "Param type": "flask.cli.SeparatedPathType",
    "Python type": "str",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_EXCLUDE_PATTERNS"
    ],
    "Default": null,
    "Is flag": false,
    "Flag value": null,
    "Is bool flag": false,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.extra_files",
    "Spec.": "--extra-files PATH",
    "Class": "click.core.Option",
    "Param type": "flask.cli.SeparatedPathType",
    "Python type": "str",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_EXTRA_FILES"
    ],
    "Default": null,
    "Is flag": false,
    "Flag value": null,
    "Is bool flag": false,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.host",
    "Spec.": "-h, --host TEXT",
    "Class": "click.core.Option",
    "Param type": "click.types.StringParamType",
    "Python type": "str",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_HOST"
    ],
    "Default": "127.0.0.1",
    "Is flag": false,
    "Flag value": null,
    "Is bool flag": false,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.key",
    "Spec.": "--key FILE",
    "Class": "click.core.Option",
    "Param type": "click.types.Path",
    "Python type": "str",
    "Hidden": false,
    "Exposed": false,
    "Env. vars.": [
      "FLASK_RUN_KEY"
    ],
    "Default": null,
    "Is flag": false,
    "Flag value": null,
    "Is bool flag": false,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.port",
    "Spec.": "-p, --port INTEGER",
    "Class": "click.core.Option",
    "Param type": "click.types.IntParamType",
    "Python type": "int",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_PORT"
    ],
    "Default": 5000,
    "Is flag": false,
    "Flag value": null,
    "Is bool flag": false,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.reload",
    "Spec.": "--reload / --no-reload",
    "Class": "click.core.Option",
    "Param type": "click.types.BoolParamType",
    "Python type": "bool",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_RELOAD"
    ],
    "Default": null,
    "Is flag": true,
    "Flag value": true,
    "Is bool flag": true,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  },
  {
    "ID": "run.with_threads",
    "Spec.": "--with-threads / --without-threads",
    "Class": "click.core.Option",
    "Param type": "click.types.BoolParamType",
    "Python type": "bool",
    "Hidden": false,
    "Exposed": true,
    "Env. vars.": [
      "FLASK_RUN_WITH_THREADS"
    ],
    "Default": true,
    "Is flag": true,
    "Flag value": true,
    "Is bool flag": true,
    "Multiple": false,
    "Nargs": 1,
    "Prompt": null,
    "Confirmation prompt": false,
    "Value": null,
    "Source": null
  }
]

Pair it with --columns to hand a consumer only the fields it reads:

$ click-extra wrap --params --table-format json --columns id,spec,envvars,default -- flask routes
[
  {
    "ID": "routes._click_default_help",
    "Spec.": "--help",
    "Env. vars.": [
      "FLASK_ROUTES__CLICK_DEFAULT_HELP"
    ],
    "Default": false
  },
  {
    "ID": "routes.all_methods",
    "Spec.": "--all-methods",
    "Env. vars.": [
      "FLASK_ROUTES_ALL_METHODS"
    ],
    "Default": false
  },
  {
    "ID": "routes.sort",
    "Spec.": "-s, --sort [endpoint|methods|domain|rule|match]",
    "Env. vars.": [
      "FLASK_ROUTES_SORT"
    ],
    "Default": "endpoint"
  }
]

Where --params describes the parameters, --help-format describes the command itself: its usage line, description, option groups, subcommands and examples. The target cooperates with neither, and needs to know nothing about Click Extra:

$ click-extra wrap --help-format json -- flask run
{
  "name": "flask run",
  "short_help": "Run a development server.",
  "version": null,
  "synopsis": "flask run [OPTIONS]",
  "description": "Run a local development server.\n\nThis server is for development purposes only. It does not provide\nthe stability, security, or performance of production WSGI servers.\n\nThe reloader and debugger are enabled by default with the '--debug'\noption.",
  "arguments": [],
  "option_groups": [
    {
      "title": null,
      "help": null,
      "options": [
        {
          "names": [
            "--debug",
            "--no-debug"
          ],
          "spec": "--debug / --no-debug",
          "metavar": null,
          "choices": null,
          "help": "Set debug mode.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--host",
            "-h"
          ],
          "spec": "--host / -h TEXT",
          "metavar": "TEXT",
          "choices": null,
          "help": "The interface to bind to.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--port",
            "-p"
          ],
          "spec": "--port / -p INTEGER",
          "metavar": "INTEGER",
          "choices": null,
          "help": "The port to bind to.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--cert"
          ],
          "spec": "--cert PATH",
          "metavar": "PATH",
          "choices": null,
          "help": "Specify a certificate file to use HTTPS.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--key"
          ],
          "spec": "--key FILE",
          "metavar": "FILE",
          "choices": null,
          "help": "The key file to use when specifying a certificate.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--reload",
            "--no-reload"
          ],
          "spec": "--reload / --no-reload",
          "metavar": null,
          "choices": null,
          "help": "Enable or disable the reloader. By default the reloader is active if debug is enabled.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--debugger",
            "--no-debugger"
          ],
          "spec": "--debugger / --no-debugger",
          "metavar": null,
          "choices": null,
          "help": "Enable or disable the debugger. By default the debugger is active if debug is enabled.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--with-threads",
            "--without-threads"
          ],
          "spec": "--with-threads / --without-threads",
          "metavar": null,
          "choices": null,
          "help": "Enable or disable multithreading.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--extra-files"
          ],
          "spec": "--extra-files PATH",
          "metavar": "PATH",
          "choices": null,
          "help": "Extra files that trigger a reload on change. Multiple paths are separated by ':'.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--exclude-patterns"
          ],
          "spec": "--exclude-patterns PATH",
          "metavar": "PATH",
          "choices": null,
          "help": "Files matching these fnmatch patterns will not trigger a reload on change. Multiple patterns are separated by ':'.",
          "required": false,
          "optional_value": false
        },
        {
          "names": [
            "--help"
          ],
          "spec": "--help",
          "metavar": null,
          "choices": null,
          "help": "Show this message and exit.",
          "required": false,
          "optional_value": false
        }
      ]
    }
  ],
  "subcommands": [],
  "examples": [],
  "environment": [],
  "files": [],
  "exit_status": [
    {
      "code": "0",
      "meaning": "Success."
    },
    {
      "code": "1",
      "meaning": "A runtime error, an aborted prompt (Ctrl-D), or a declined confirmation."
    },
    {
      "code": "2",
      "meaning": "A usage error: unknown option, invalid value, missing operand, or an unparsable configuration file."
    },
    {
      "code": "130",
      "meaning": "Interrupted with Ctrl-C: the process ends by SIGINT, reported as 130."
    }
  ]
}

Feeding a CLI to an agent¶

Three things are worth knowing before wiring any of this into a tool or a model.

Hand a model Markdown, not JSON. Both carry the same content, and JSON is the right answer for code that indexes fields. But a model reads prose better than it reads a nested object, and pays fewer tokens for it: no quoting, no punctuation scaffolding, and headings it already knows how to skim.

Descend, do not dump. The plain formats name a command’s children without expanding them, so an agent answering a question about one leaf spends its context on that leaf rather than on a tree it will not read. Reach for -full when the whole surface is the point (generating documentation, diffing two releases), not by default. On a large CLI the difference is the whole budget.

Two lookalikes that answer different questions. --help-format json describes the interface: what the command is, what it accepts, what it documents. --params describes the state of one invocation: every parameter’s resolved value and where it came from, be that a flag, an environment variable, a configuration file or a default. An agent picking flags wants the first. An agent debugging why a run behaved a certain way wants the second.

Neither requires the target to cooperate: click-extra wrap extracts both from a CLI that knows nothing about Click Extra.