From 758ddc47eefdb3b02ae3be1be92dff3d2605089d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6hm?= Date: Fri, 5 Jan 2024 17:03:48 +0100 Subject: [PATCH 1/3] Add rudimentary support for click.MultiCommand --- trogon/introspect.py | 7 +++++++ trogon/trogon.py | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/trogon/introspect.py b/trogon/introspect.py index b0855ed..6e01c29 100644 --- a/trogon/introspect.py +++ b/trogon/introspect.py @@ -165,6 +165,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 1238747..8345353 100644 --- a/trogon/trogon.py +++ b/trogon/trogon.py @@ -67,7 +67,8 @@ def __init__( super().__init__(name, id, classes) self.command_data = None self.cli = cli - self.is_grouped_cli = isinstance(cli, click.Group) + self.is_grouped_cli = (isinstance(cli, click.Group) or + isinstance(cli, click.MultiCommand)) self.command_schemas = introspect_click_app(cli) self.click_app_name = click_app_name self.command_name = command_name From 94c4eaa9fd44a3e217b1486b49a67bdb03787b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6hm?= Date: Fri, 5 Jan 2024 17:13:15 +0100 Subject: [PATCH 2/3] Make multi commands appear visually like groups --- trogon/introspect.py | 6 +++++- trogon/trogon.py | 7 +++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/trogon/introspect.py b/trogon/introspect.py index 6e01c29..ad266d9 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: diff --git a/trogon/trogon.py b/trogon/trogon.py index 8345353..46d2ff6 100644 --- a/trogon/trogon.py +++ b/trogon/trogon.py @@ -27,7 +27,7 @@ from trogon.detect_run_string import detect_run_string from trogon.introspect import ( introspect_click_app, - CommandSchema, + CommandSchema, is_grouped_command, ) from trogon.run_command import UserCommandData from trogon.widgets.command_info import CommandInfo @@ -67,8 +67,7 @@ def __init__( super().__init__(name, id, classes) self.command_data = None self.cli = cli - self.is_grouped_cli = (isinstance(cli, click.Group) or - isinstance(cli, click.MultiCommand)) + 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 @@ -226,7 +225,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() From 86a2f3e852160a07780d5d2364cdef5f005705e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6hm?= Date: Fri, 5 Jan 2024 17:16:51 +0100 Subject: [PATCH 3/3] Reformat imports --- trogon/trogon.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/trogon/trogon.py b/trogon/trogon.py index 46d2ff6..a3cc823 100644 --- a/trogon/trogon.py +++ b/trogon/trogon.py @@ -27,7 +27,8 @@ from trogon.detect_run_string import detect_run_string from trogon.introspect import ( introspect_click_app, - CommandSchema, is_grouped_command, + CommandSchema, + is_grouped_command, ) from trogon.run_command import UserCommandData from trogon.widgets.command_info import CommandInfo