"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix: respect explicit click names in skill generation
陈家名6ff6ceb68974
diff --git a/cli-anything-plugin/skill_generator.py b/cli-anything-plugin/skill_generator.pyindex 80aeb53cf..07b913318 100644--- a/cli-anything-plugin/skill_generator.py+++ b/cli-anything-plugin/skill_generator.py@@ -30,6 +30,16 @@ def _canonical_skill_name(harness_path: Path, software_name: str) -> str:return f"cli-anything-{software_dir.replace('_', '-')}"+def _click_declared_name(decorator_args: str, fallback: str) -> str:+ """Return the Click command/group name declared in a decorator."""+ match = re.search(r'^\s*["\']([^"\']+)["\']', decorator_args)+ if not match:+ match = re.search(r'\bname\s*=\s*["\']([^"\']+)["\']', decorator_args)+ if match:+ return match.group(1)+ return fallback.replace("_", "-")++@dataclassclass CommandInfo:"""Information about a CLI command."""@@ -210,27 +220,29 @@ def extract_commands_from_cli(cli_path: Path) -> list[CommandGroup]:# - Various Click decorator patterns like @click.option(), @click.argument()# Uses re.DOTALL to match across newlines between decorator and defgroup_pattern = (- r'@(\w+)\.group\([^)]*\)' # @xxx.group(...)+ r'@(\w+)\.group\(([^)]*)\)' # @xxx.group(...)r'(?:\s*@[\w.]+\([^)]*\))*' # optional additional decoratorsr'\s*def\s+(\w+)\([^)]*\)' # def xxx(...):r':\s*' # colon with optional whitespacer'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?' # optional docstring (""" or '''))+ group_lookup = {}for match in re.finditer(group_pattern, content):- group_func = match.group(2)- # Docstring can be in group 3 (triple-double) or group 4 (triple-single)- group_doc = (match.group(3) or match.group(4) or "").strip()+ group_args = match.group(2)+ group_func = match.group(3)+ # Docstring can be in group 4 (triple-double) or group 5 (triple-single)+ group_doc = (match.group(4) or match.group(5) or "").strip()- group_name = group_func.replace("_", " ").title()- if not group_name:- group_name = group_func.title()+ group_name = _format_display_name(_click_declared_name(group_args, group_func))- groups.append(CommandGroup(+ group = CommandGroup(name=group_name,description=group_doc or f"Commands for {group_name.lower()} operations.",commands=[]- ))+ )+ groups.append(group)+ group_lookup[group_func.lower()] = group# Find Click command decorators# Pattern handles:@@ -238,7 +250,7 @@ def extract_commands_from_cli(cli_path: Path) -> list[CommandGroup]:# - Docstrings on the same line or following line after function definition# - Various Click decorator patterns like @click.option(), @click.argument()command_pattern = (- r'@(\w+)\.command\([^)]*\)' # @xxx.command(...)+ r'@(\w+)\.command\(([^)]*)\)' # @xxx.command(...)r'(?:\s*@[\w.]+\([^)]*\))*' # optional additional decoratorsr'\s*def\s+(\w+)\([^)]*\)' # def xxx(...):r':\s*' # colon with optional whitespace@@ -247,17 +259,19 @@ def extract_commands_from_cli(cli_path: Path) -> list[CommandGroup]:for match in re.finditer(command_pattern, content):group_name = match.group(1)- cmd_name = match.group(2)- # Docstring can be in group 3 (triple-double) or group 4 (triple-single)- cmd_doc = (match.group(3) or match.group(4) or "").strip()+ cmd_args = match.group(2)+ cmd_func = match.group(3)+ # Docstring can be in group 4 (triple-double) or group 5 (triple-single)+ cmd_doc = (match.group(4) or match.group(5) or "").strip()# Find the matching group- for group in groups:- if group.name.lower().replace(" ", "_") == group_name.lower():- group.commands.append(CommandInfo(- name=cmd_name.replace("_", "-"),- description=cmd_doc or f"Execute {cmd_name} operation."- ))+ group = group_lookup.get(group_name.lower())+ if group:+ cmd_name = _click_declared_name(cmd_args, cmd_func)+ group.commands.append(CommandInfo(+ name=cmd_name,+ description=cmd_doc or f"Execute {cmd_func} operation."+ ))# If no groups found, create a default one with all commandsif not groups:@@ -268,12 +282,14 @@ def extract_commands_from_cli(cli_path: Path) -> list[CommandGroup]:)for match in re.finditer(command_pattern, content):- cmd_name = match.group(2)- # Docstring can be in group 3 (triple-double) or group 4 (triple-single)- cmd_doc = (match.group(3) or match.group(4) or "").strip()+ cmd_args = match.group(2)+ cmd_func = match.group(3)+ # Docstring can be in group 4 (triple-double) or group 5 (triple-single)+ cmd_doc = (match.group(4) or match.group(5) or "").strip()+ cmd_name = _click_declared_name(cmd_args, cmd_func)default_group.commands.append(CommandInfo(- name=cmd_name.replace("_", "-"),- description=cmd_doc or f"Execute {cmd_name} operation."+ name=cmd_name,+ description=cmd_doc or f"Execute {cmd_func} operation."))if default_group.commands:diff --git a/cli-anything-plugin/tests/test_skill_generator.py b/cli-anything-plugin/tests/test_skill_generator.pyindex c2f7216d5..35ea4d4b3 100644--- a/cli-anything-plugin/tests/test_skill_generator.py+++ b/cli-anything-plugin/tests/test_skill_generator.py@@ -143,6 +143,46 @@ class TestExtractCliMetadata:assert "export" in cmd_namesassert "import-data" in cmd_names+ def test_respects_explicit_click_names(self, tmp_path):+ software = "named"+ cli_pkg = tmp_path / "cli_anything" / software+ cli_pkg.mkdir(parents=True)+ (cli_pkg / "__init__.py").write_text("")+ (cli_pkg / f"{software}_cli.py").write_text(+ textwrap.dedent("""\+ import click++ @click.group()+ def cli():+ pass++ @cli.group("remote-access")+ def remote_access_group():+ \"\"\"Remote access commands.\"\"\"+ pass++ @remote_access_group.command("list-active")+ def list_active_sessions():+ \"\"\"List active sessions.\"\"\"+ pass++ @cli.command(name="health-check")+ def health():+ \"\"\"Check service health.\"\"\"+ pass+ """)+ )++ metadata = extract_cli_metadata(str(tmp_path))+ groups = {group.name: group for group in metadata.command_groups}++ assert "Remote Access" in groups+ assert "Remote Access Group" not in groups+ assert [cmd.name for cmd in groups["Remote Access"].commands] == ["list-active"]+ cli_commands = [cmd.name for cmd in groups["Cli"].commands]+ assert "health-check" in cli_commands+ assert "health" not in cli_commands+def test_generates_examples(self, harness_dir):metadata = extract_cli_metadata(str(harness_dir))assert len(metadata.examples) > 0