Command treeΒΆ

The --tree flag prints the whole hierarchy of nested subcommands at once, with their one-line descriptions. It is a constant-cost overview of a CLI’s shape: without it, mapping an unfamiliar command set means walking --help screens one level at a time, or generating a full man page or completion spec dump.

It is also the hierarchical companion of --params: where the parameter table flattens the command tree into dotted IDs (cli.subcommand.param), --tree renders the skeleton those IDs hang off, without the parameter noise.

--tree flagΒΆ

The flag is part of the default options, so every CLI built with @command or @group exposes it:

from click_extra import argument, group

@group
def observatory():
    """Weather observatory control center."""

@observatory.group(aliases=["st"])
def station():
    """Manage remote weather stations."""

@station.command()
def calibrate():
    """Recalibrate the sensors."""

@station.command(deprecated=True)
def reboot():
    """Power-cycle the station."""

@observatory.command()
@argument("city")
def report(city):
    """Print the forecast for a city."""

@observatory.command()
def status():
    """Report the current conditions."""

@observatory.command(hidden=True)
def diagnose():
    """Internal diagnostics."""
$ observatory --tree
observatory                     Weather observatory control center.
β”œβ”€β”€ help [COMMAND_PATH]...      Show help for a command.
β”œβ”€β”€ report CITY                 Print the forecast for a city.
β”œβ”€β”€ station (st)                Manage remote weather stations.
β”‚   β”œβ”€β”€ calibrate               Recalibrate the sensors.
β”‚   β”œβ”€β”€ help [COMMAND_PATH]...  Show help for a command.
β”‚   └── reboot                  Power-cycle the station. (DEPRECATED)
└── status                      Report the current conditions.

The rendering mirrors help screens: subcommands are discovered dynamically (so lazily-registered commands are included), listed in the same order, hidden ones are skipped, aliases are parenthesized, operand metavars follow each name like on the usage line (report CITY), and deprecated commands carry Click’s own (DEPRECATED) label. Descriptions are column-aligned and wrap at the terminal width, with the tree rail running through the wrapped lines. Everything is styled with the same theme slots as help screens (invoked_command for the root, subcommand and alias below, metavar for operands, deprecated for that label), so the tree follows --theme and --color like every other output.

Options are deliberately absent from the tree: their exhaustive inventory is --params’ job.

Like the completion spec, the tree is a point-in-time snapshot: a group that computes its subcommands from external state (a loaded application, installed plugins, a scanned directory) renders exactly what its list_commands returns at that moment.

AccessibilityΒΆ

Box-drawing characters are hostile to screen readers, which either skip them or spell out their Unicode names. Under accessibility mode, the rail degrades to the pure-ASCII set tree(1) uses with --charset=ascii:

$ observatory --accessible --tree
observatory                     Weather observatory control center.
|-- help [COMMAND_PATH]...      Show help for a command.
|-- report CITY                 Print the forecast for a city.
|-- station (st)                Manage remote weather stations.
|   |-- calibrate               Recalibrate the sensors.
|   |-- help [COMMAND_PATH]...  Show help for a command.
|   `-- reboot                  Power-cycle the station. (DEPRECATED)
`-- status                      Report the current conditions.

Foreign CLIsΒΆ

click-extra wrap --tree -- SCRIPT resolves a target, loads its Click command, and prints its tree without running it. SCRIPT is resolved the same way as for the other introspection modes, so with uvx nothing needs to be installed up front:

$ uvx --from click-extra --with flask click-extra wrap --tree -- flask
$ click-extra wrap --tree -- flask
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

flask       A general utility script for Flask applications.
β”œβ”€β”€ routes  Show the routes for the app.
β”œβ”€β”€ run     Run a development server.
└── shell   Run a shell in the app context.

The error line above is Flask itself: its group computes part of its command list from the application, and without one only the static commands render. That is the point-in-time snapshot caveat made visible.

Extra arguments after SCRIPT navigate into nested command groups and re-root the tree at the resolved node, exactly like subcommand drilling for --params:

$ click-extra wrap --tree -- flask run

Structure vs. inventoryΒΆ

--tree and --params are two projections of the same walk, and answer different questions:

  • --tree is command-level, hierarchical, static: which subcommands exist and how they nest. No values, no options.

  • --params is parameter-level, flat, runtime: one row per parameter with its type, environment variable, default, current value and provenance.

The tree is a human rendering only. Machine-readable hierarchies are covered elsewhere: --help-format json-full walks the whole tree as JSON, --params --table-format json serializes every parameter with its dotted path, and --help-format carapace emits the command-and-flag tree as YAML.

Standalone optionΒΆ

Use the @tree_option decorator to add the flag to a plain Click CLI, outside of Click Extra’s @command and @group:

import click

from click_extra import tree_option

@click.group
@tree_option
def produce():
    """Manage a produce stand."""

@produce.command()
def restock():
    """Restock the shelves."""
$ produce --tree
produce      Manage a produce stand.
└── restock  Restock the shelves.