diff --git a/trogon/introspect.py b/trogon/introspect.py index d200fa4..b00aef2 100644 --- a/trogon/introspect.py +++ b/trogon/introspect.py @@ -12,6 +12,10 @@ def generate_unique_id(): return f"id_{str(uuid.uuid4())[:8]}" +def is_grouped_command(cmd: BaseCommand): + return isinstance(cmd, click.Group) or isinstance(cmd, click.MultiCommand) + + @dataclass class MultiValueParamData: values: list[tuple[int | float | str]] @@ -123,7 +127,7 @@ def process_command( arguments=[], subcommands={}, parent=parent, - is_group=isinstance(cmd_obj, click.Group), + is_group=is_grouped_command(cmd_obj), ) for param in cmd_obj.params: @@ -165,6 +169,13 @@ def process_command( cmd_data.subcommands[CommandName(subcmd_name)] = process_command( CommandName(subcmd_name), subcmd_obj, parent=cmd_data ) + elif isinstance(cmd_obj, click.MultiCommand): + for subcmd_name in cmd_obj.list_commands(None): + cmd_data.subcommands[CommandName(subcmd_name)] = process_command( + CommandName(subcmd_name), + cmd_obj.get_command(None, subcmd_name), + parent=cmd_data + ) return cmd_data diff --git a/trogon/trogon.py b/trogon/trogon.py index e2770ed..896855c 100644 --- a/trogon/trogon.py +++ b/trogon/trogon.py @@ -29,6 +29,7 @@ from trogon.introspect import ( introspect_click_app, CommandSchema, + is_grouped_command, CommandName, ) from trogon.run_command import UserCommandData @@ -73,7 +74,7 @@ def __init__( super().__init__(name, id, classes) self.command_data: UserCommandData = UserCommandData(CommandName("_default")) self.cli = cli - self.is_grouped_cli = isinstance(cli, click.Group) + self.is_grouped_cli = is_grouped_command(cli) self.command_schemas = introspect_click_app(cli) self.click_app_name = click_app_name self.command_name = command_name @@ -216,7 +217,7 @@ def __init__( super().__init__() self.cli = cli self.post_run_command: list[str] = [] - self.is_grouped_cli = isinstance(cli, click.Group) + self.is_grouped_cli = is_grouped_command(cli) self.execute_on_exit = False if app_name is None and click_context is not None: self.app_name = detect_run_string()