From 308166175e67e4775b9e0edeb4499c353e424508 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Sun, 12 May 2024 23:43:23 +0900 Subject: [PATCH 1/2] improve: support basic auto completion in `comfy node` commands. https://github.com/yoland68/comfy-cli/issues/46 --- comfy_cli/command/custom_nodes/command.py | 197 ++++++++++++++++------ 1 file changed, 143 insertions(+), 54 deletions(-) diff --git a/comfy_cli/command/custom_nodes/command.py b/comfy_cli/command/custom_nodes/command.py index 948dc55..b99a307 100644 --- a/comfy_cli/command/custom_nodes/command.py +++ b/comfy_cli/command/custom_nodes/command.py @@ -168,20 +168,49 @@ def clear(path: str): execute_cm_cli(["clear", path]) +def show_completer(incomplete: str) -> List[str]: + valid_choices = [ + "installed", + "enabled", + "not-installed", + "disabled", + "all", + "snapshot", + "snapshot-list", + ] + return [choice for choice in valid_choices if choice.startswith(incomplete)] + + +def mode_completion(incomplete: str) -> list[str]: + modes = ["remote", "local", "cache"] + return [mode for mode in modes if mode.startswith(incomplete)] + + +def channel_completion(incomplete: str) -> list[str]: + modes = ["default", "recent", "dev", "forked", "tutorial", "legacy"] + return [mode for mode in modes if mode.startswith(incomplete)] + + @app.command(help="Show node list") @tracking.track_command("node") def show( - args: List[str] = typer.Argument( - ..., + arg: str = typer.Argument( help="[installed|enabled|not-installed|disabled|all|snapshot|snapshot-list]", + autocompletion=show_completer, ), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_commands = [ "installed", @@ -192,8 +221,8 @@ def show( "snapshot", "snapshot-list", ] - if not args or len(args) > 1 or args[0] not in valid_commands: - typer.echo(f"Invalid command: `show {' '.join(args)}`", err=True) + if arg not in valid_commands: + typer.echo(f"Invalid command: `show {arg}`", err=True) raise typer.Exit(code=1) valid_modes = ["remote", "local", "cache"] @@ -204,23 +233,29 @@ def show( ) raise typer.Exit(code=1) - execute_cm_cli(["show"] + args, channel, mode) + execute_cm_cli(["show", arg], channel, mode) @app.command("simple-show", help="Show node list (simple mode)") @tracking.track_command("node") def simple_show( - args: List[str] = typer.Argument( - ..., + arg: str = typer.Argument( help="[installed|enabled|not-installed|disabled|all|snapshot|snapshot-list]", + autocompletion=show_completer, ), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_commands = [ "installed", @@ -231,8 +266,8 @@ def simple_show( "snapshot", "snapshot-list", ] - if not args or len(args) > 1 or args[0] not in valid_commands: - typer.echo(f"Invalid command: `show {' '.join(args)}`", err=True) + if arg not in valid_commands: + typer.echo(f"Invalid command: `show {arg}`", err=True) raise typer.Exit(code=1) valid_modes = ["remote", "local", "cache"] @@ -243,7 +278,7 @@ def simple_show( ) raise typer.Exit(code=1) - execute_cm_cli(["simple-show"] + args, channel, mode) + execute_cm_cli(["simple-show", arg], channel, mode) # install, reinstall, uninstall @@ -253,11 +288,17 @@ def install( args: List[str] = typer.Argument(..., help="install custom nodes"), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): if "all" in args: typer.echo(f"Invalid command: {mode}. `install all` is not allowed", err=True) @@ -280,11 +321,17 @@ def reinstall( args: List[str] = typer.Argument(..., help="reinstall custom nodes"), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): if "all" in args: typer.echo(f"Invalid command: {mode}. `reinstall all` is not allowed", err=True) @@ -307,11 +354,17 @@ def uninstall( args: List[str] = typer.Argument(..., help="uninstall custom nodes"), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): if "all" in args: typer.echo(f"Invalid command: {mode}. `uninstall all` is not allowed", err=True) @@ -337,11 +390,17 @@ def update( args: List[str] = typer.Argument(..., help="update custom nodes"), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_modes = ["remote", "local", "cache"] if mode and mode.lower() not in valid_modes: @@ -360,11 +419,17 @@ def disable( args: List[str] = typer.Argument(..., help="disable custom nodes"), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_modes = ["remote", "local", "cache"] if mode and mode.lower() not in valid_modes: @@ -383,11 +448,17 @@ def enable( args: List[str] = typer.Argument(..., help="enable custom nodes"), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_modes = ["remote", "local", "cache"] if mode and mode.lower() not in valid_modes: @@ -408,11 +479,17 @@ def fix( ), channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_modes = ["remote", "local", "cache"] if mode and mode.lower() not in valid_modes: @@ -439,11 +516,17 @@ def install_deps( ] = None, channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_modes = ["remote", "local", "cache"] if mode and mode.lower() not in valid_modes: @@ -497,11 +580,17 @@ def deps_in_workflow( ], channel: Annotated[ str, - typer.Option(show_default=False, help="Specify the operation mode"), - ] = None, - mode: Annotated[ - str, typer.Option(show_default=False, help="[remote|local|cache]") + typer.Option( + show_default=False, + help="Specify the operation mode", + autocompletion=channel_completion, + ), ] = None, + mode: str = typer.Option( + None, + help="[remote|local|cache]", + autocompletion=mode_completion, + ), ): valid_modes = ["remote", "local", "cache"] if mode and mode.lower() not in valid_modes: From 4fb44a656dd0e550b67aedac35109c15e4003205 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Sun, 12 May 2024 23:48:00 +0900 Subject: [PATCH 2/2] refactor --- comfy_cli/command/custom_nodes/command.py | 50 +++++++++++------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/comfy_cli/command/custom_nodes/command.py b/comfy_cli/command/custom_nodes/command.py index b99a307..f08c346 100644 --- a/comfy_cli/command/custom_nodes/command.py +++ b/comfy_cli/command/custom_nodes/command.py @@ -168,7 +168,7 @@ def clear(path: str): execute_cm_cli(["clear", path]) -def show_completer(incomplete: str) -> List[str]: +def show_completer(incomplete: str) -> list[str]: valid_choices = [ "installed", "enabled", @@ -181,12 +181,12 @@ def show_completer(incomplete: str) -> List[str]: return [choice for choice in valid_choices if choice.startswith(incomplete)] -def mode_completion(incomplete: str) -> list[str]: +def mode_completer(incomplete: str) -> list[str]: modes = ["remote", "local", "cache"] return [mode for mode in modes if mode.startswith(incomplete)] -def channel_completion(incomplete: str) -> list[str]: +def channel_completer(incomplete: str) -> list[str]: modes = ["default", "recent", "dev", "forked", "tutorial", "legacy"] return [mode for mode in modes if mode.startswith(incomplete)] @@ -203,13 +203,13 @@ def show( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_commands = [ @@ -248,13 +248,13 @@ def simple_show( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_commands = [ @@ -291,13 +291,13 @@ def install( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): if "all" in args: @@ -324,13 +324,13 @@ def reinstall( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): if "all" in args: @@ -357,13 +357,13 @@ def uninstall( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): if "all" in args: @@ -393,13 +393,13 @@ def update( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_modes = ["remote", "local", "cache"] @@ -422,13 +422,13 @@ def disable( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_modes = ["remote", "local", "cache"] @@ -451,13 +451,13 @@ def enable( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_modes = ["remote", "local", "cache"] @@ -482,13 +482,13 @@ def fix( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_modes = ["remote", "local", "cache"] @@ -519,13 +519,13 @@ def install_deps( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_modes = ["remote", "local", "cache"] @@ -583,13 +583,13 @@ def deps_in_workflow( typer.Option( show_default=False, help="Specify the operation mode", - autocompletion=channel_completion, + autocompletion=channel_completer, ), ] = None, mode: str = typer.Option( None, help="[remote|local|cache]", - autocompletion=mode_completion, + autocompletion=mode_completer, ), ): valid_modes = ["remote", "local", "cache"]