From bbc24f585dbc908ab243d6c6d4dba825e16144f7 Mon Sep 17 00:00:00 2001 From: Philipp Temminghoff Date: Tue, 19 Sep 2023 23:15:32 +0200 Subject: [PATCH] chore: cli work --- mknodes/utils/clihelpers.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/mknodes/utils/clihelpers.py b/mknodes/utils/clihelpers.py index 775b1870..a443aa09 100644 --- a/mknodes/utils/clihelpers.py +++ b/mknodes/utils/clihelpers.py @@ -63,13 +63,22 @@ class CommandInfo: def __getitem__(self, name): return self.subcommands[name] - def to_markdown(self): + def to_markdown(self, recursive: bool = False): text = self.description + "\n\n" + str(mkcode.MkCode(self.usage)) params = [i.to_markdown() for i in self.params] - return text + "\n\n\n" + "\n\n\n".join(params) - - -def get_typer_info(instance: typer.Typer, command: str | None = None) -> CommandInfo: + cmd_text = text + "\n\n\n" + "\n\n\n".join(params) + if not recursive: + return cmd_text + children_text = "\n".join( + i.to_markdown(recursive=True) for i in self.subcommands.values() + ) + return cmd_text + children_text + + +def get_typer_info( + instance: typer.Typer | click.Group, + command: str | None = None, +) -> CommandInfo: cmd = get_command(instance) if isinstance(instance, typer.Typer) else instance info = get_command_info(cmd) if command: @@ -118,7 +127,7 @@ def _make_usage(ctx: click.Context) -> str: if __name__ == "__main__": from pprint import pprint - from mknodes import cli + import mkdocs.__main__ - info = get_typer_info(cli.cli, "build") - pprint(info.to_markdown()) + info = get_typer_info(mkdocs.__main__.cli) + pprint(info.to_markdown(recursive=True))