click-command-tree
click-command-tree is a Click plugin that provides a `tree` command to any Click CLI application, allowing users to visualize the command hierarchy. It is actively maintained, with version 1.2.0 released on March 24, 2024, and maintains a consistent release cadence for updates and features.
Common errors
-
Error: No such command 'tree'.
cause The `click-command-tree` plugin was not correctly registered with your Click application's root group, or the `click-plugins` dependency is missing.fixEnsure you have `click-plugins` installed (`pip install click-plugins`) and that your root `click.group` is decorated with `@with_plugins(importlib.metadata.entry_points(group="click_command_tree"))` (or `pkg_resources` for older Python). -
AttributeError: module 'pkg_resources' has no attribute 'iter_entry_points'
cause `pkg_resources` is deprecated, and its `iter_entry_points` function may not be available or function as expected in newer Python environments, particularly Python 3.8+.fixUpdate your code to use `importlib.metadata.entry_points` instead of `pkg_resources.iter_entry_points`. For example: `import importlib.metadata` and then `@with_plugins(importlib.metadata.entry_points(group="click_command_tree"))`.
Warnings
- breaking The `tree` command no longer displays commands marked with `hidden=True` in their Click definition.
- breaking The output format of the `tree` command was altered to include docstrings for commands and groups.
- gotcha Correctly registering the plugin requires using `click-plugins` with the appropriate entry point group, and the method for retrieving entry points (`importlib.metadata` vs `pkg_resources`) varies by Python version.
Install
-
pip install click-command-tree
Imports
- click
import click
- with_plugins
from click_plugins import with_plugins
- importlib.metadata.entry_points
@with_plugins(pkg_resources.iter_entry_points('click_command_tree'))import importlib.metadata @with_plugins(importlib.metadata.entry_points(group='click_command_tree'))
Quickstart
import click
from click_plugins import with_plugins
import importlib.metadata
@with_plugins(importlib.metadata.entry_points(group="click_command_tree"))
@click.group()
def cli():
"""A sample CLI to demonstrate click-command-tree.
Try: `python your_cli.py tree`
"""
pass
@cli.group()
def users():
"""Manage users.
"""
pass
@users.command(name="add")
def add_user():
"""Add a new user.
"""
click.echo("Adding user...")
@users.command(hidden=True)
def delete_user():
"""Delete a user (hidden command).
"""
click.echo("Deleting user...")
@cli.command()
def config():
"""Configure settings.
"""
click.echo("Configuring settings...")
if __name__ == '__main__':
cli()