88
99from cli import __version__
1010from cli .utils .config import CLIConfig , set_config , get_config
11+ from cli .utils .suggestions import suggest_matches , format_suggestions
1112from cli .utils .output import format_output , print_error , print_success , print_info
1213from cli .utils .connection import (
1314 run_command ,
@@ -28,6 +29,35 @@ def __init__(self):
2829pass_context = click .make_pass_decorator (Context , ensure = True )
2930
3031
32+ _ORIGINAL_RESOLVE_COMMAND = click .Group .resolve_command
33+
34+
35+ def _resolve_command_with_suggestions (self : click .Group , ctx : click .Context , args : list [str ]):
36+ try :
37+ return _ORIGINAL_RESOLVE_COMMAND (self , ctx , args )
38+ except click .exceptions .NoSuchCommand as e :
39+ if not args or args [0 ].startswith ("-" ):
40+ raise
41+ matches = suggest_matches (args [0 ], self .list_commands (ctx ))
42+ suggestion = format_suggestions (matches )
43+ if suggestion :
44+ message = f"{ e } \n { suggestion } "
45+ raise click .exceptions .UsageError (message , ctx = ctx )
46+ raise
47+ except click .exceptions .UsageError as e :
48+ if args and not args [0 ].startswith ("-" ) and "No such command" in str (e ):
49+ matches = suggest_matches (args [0 ], self .list_commands (ctx ))
50+ suggestion = format_suggestions (matches )
51+ if suggestion :
52+ message = f"{ e } \n { suggestion } "
53+ raise click .exceptions .UsageError (message , ctx = ctx )
54+ raise
55+
56+
57+ # Install suggestion handling for all CLI command groups.
58+ click .Group .resolve_command = _resolve_command_with_suggestions # type: ignore[assignment]
59+
60+
3161@click .group ()
3262@click .version_option (version = __version__ , prog_name = "unity-mcp" )
3363@click .option (
@@ -212,6 +242,8 @@ def register_optional_command(module_name: str, command_name: str) -> None:
212242 cli .add_command (command )
213243
214244 optional_commands = [
245+ ("cli.commands.tool" , "tool" ),
246+ ("cli.commands.tool" , "custom_tool" ),
215247 ("cli.commands.gameobject" , "gameobject" ),
216248 ("cli.commands.component" , "component" ),
217249 ("cli.commands.scene" , "scene" ),
0 commit comments