import click # Click Plugins # =============================== class NestedHelpGroup(click.Group): # class NestedHelpGroup(RichGroup): """This class provides a way to show all commands of all children instead of just the parent. Optionnaly accept to hides groups or not. """ def get_command(self, ctx, cmd_name: str): """Given a context and a command name, this returns a :class:`Command` object if it exists or returns ``None``. """ # Resolve name part by part parts = cmd_name.split(" ") curr = self for idx, part in enumerate(parts): curr = curr.commands.get(part) return curr def list_commands(self, ctx) -> list[str]: "List all children commands" return self._resolve_children(self, ctx=ctx, ignore_groups=True) @classmethod def _resolve_children(cls, obj, ctx=None, ignore_groups=False, _parent=None): "Resolve recursively all children" # Source: Adapted from https://stackoverflow.com/a/56159096 if isinstance(obj, click.Group): ret = [] for name, child in obj.commands.items(): # Build full name full_name = name if _parent: full_name = " ".join([_parent, name]) # Save records record = full_name if ignore_groups: if not isinstance(child, click.Group): ret.append(record) else: ret.append(record) # Recursive loop ret.extend( cls._resolve_children( child, ctx=ctx, ignore_groups=ignore_groups, _parent=full_name ) ) return ret return []