click_extra.tree module¶
classDiagram
ExtraOption <|-- TreeOption
Render Click command hierarchies as trees.
Provides the --tree flag: a constant-cost overview of a CLI’s nested
subcommands, where the only alternative is walking --help screens one
level at a time or generating a full man page or completion spec dump.
This is the command-level, hierarchical companion of --params:
where the parameter table flattens the hierarchy into dotted id paths
(cli.subcommand.param), the tree renders the skeleton those paths hang
off. Both share the same dynamic discovery (click.Group.list_commands()
/ click.Group.get_command() via
iter_subcommands()) and the same one-line
descriptions (full_short_help()).
The tree is a human rendering only: machine-readable hierarchies are already
served by --params --table-format json (parameters with dotted
paths) and the Carapace exporter (click_extra.carapace, a YAML
command-and-flag tree).
Subcommands are discovered through a live-context walk, so lazily-registered commands are included, and the view hides behind a dedicated eager flag, so the plain help screen stays untouched. See Upstream for how this compares to the tree views of neighboring packages.
- class click_extra.tree.TreeGlyphs(branch, last, pipe, space)[source]¶
Bases:
objectThe four drawing strings composing a tree’s left-hand rail.
- click_extra.tree.BOX_GLYPHS = TreeGlyphs(branch='├── ', last='└── ', pipe='│ ', space=' ')¶
Unicode box-drawing rail, the same set drawn by
tree(1).The default table format (
rounded-outline) already emits box-drawing characters, so the tree matches the rest of Click Extra’s terminal output.
- click_extra.tree.ASCII_GLYPHS = TreeGlyphs(branch='|-- ', last='`-- ', pipe='| ', space=' ')¶
Pure-ASCII rail, the same set drawn by
tree(1)under--charset=ascii.Selected when accessibility mode is active (
ACCESSIBLE): box-drawing characters are hostile to screen readers, which either skip them or spell out their Unicode names.
- click_extra.tree.COLUMN_GAP = 2¶
Blank cells between the tree rail column and the description column.
- click_extra.tree.MIN_DESCRIPTION_WIDTH = 20¶
Floor for the description column’s wrap width.
A deep tree with long labels can push the description column close to (or past) the terminal’s right edge. Below this floor, wrapping would shred sentences into confetti, so the column keeps this width and overflows the terminal instead.
- click_extra.tree.render_command_tree(command, prog_name=None, ctx=None, width=None)[source]¶
Render the hierarchy rooted at
commandas a tree with descriptions.Reuses
ctxwhen given (like the live invocation context), otherwise builds a throwaway one withresilient_parsing=True. The root line is labeled withprog_namewhen given, else the context’s command path.Each node carries the command name, its aliases, its operand metavars (mirroring the usage line) and a column-aligned description from
full_short_help(), so deprecated commands carry Click’s own(DEPRECATED)label. Everything is styled with the same theme slots as help screens (invoked_commandfor the root,subcommandandaliasfor children,metavarfor operands,deprecatedfor that label), so the tree follows--themeand--color. The rail switches from box-drawing to ASCII when accessibility mode is active on the context (seeACCESSIBLE).Descriptions wrap at
width, resolved like help screens when not given (ctx.make_formatter(), honoring theterminal_widthandmax_content_widthcontext settings). Wrapped lines keep the tree rail running through the description column. A label wider than the column (typically a long script path as the root, underwrap --tree) keeps its line to itself and hangs its description underneath, at the column.Note
The tree is a point-in-time snapshot: groups computing their subcommands from external state (plugins, scanned directories) render exactly what
click.Group.list_commands()returns at that moment, in listing order. Cloup section groupings are not rendered.Caution
A non-group command renders as a single root line: valid, just not very interesting. Like a man page, the output stays meaningful for every command type.
- Return type:
- class click_extra.tree.TreeOption(param_decls=None, *, is_flag=True, expose_value=False, is_eager=True, help='Show the tree of nested subcommands and exit.', **kwargs)[source]¶
Bases:
ExtraOptionA pre-configured
--treeflag that prints the hierarchy of nested subcommands and exits.Eager and value-less, like
ManOption. Part of the default option set injected bydefault_params(), so every@commandand@groupexposes it. Use@tree_optionto add it to a plain Click CLI.Note
The flag is named
--tree, not--show-treeor--commands.Rendering a hierarchy as a tree is conventionally named
treeacross ecosystems:eza --tree,lsblk --tree,poetry show --tree,cargo tree,pstree(1)andtree(1)itself (ps --forestbeing the lone dissenter).--commandswould suggest the flat listing--helpalready provides.The bare noun also lines up with the neighbouring
--help,--man,--paramsand--versioninformational flags. It is a flag rather than a registeredtreesubcommand, which could collide with the user’s own command namespace.