Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for click.MultiCommand #71

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion trogon/introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions trogon/trogon.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from trogon.introspect import (
introspect_click_app,
CommandSchema,
is_grouped_command,
CommandName,
)
from trogon.run_command import UserCommandData
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down