Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(review): detect decorated Click root groups

陈家名88ef85726ae8
diff --git a/cli-anything-plugin/skill_generator.py b/cli-anything-plugin/skill_generator.py
index 8d5dbf95f..1f754fadf 100644
--- a/cli-anything-plugin/skill_generator.py
+++ b/cli-anything-plugin/skill_generator.py
@@ -219,12 +219,14 @@ 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()
# Uses re.DOTALL to match across newlines between decorator and def
+ optional_decorator_pattern = r'(?:\s*@[\w.]+(?:\([^)]*\))?)*'
+
group_pattern = (
- r'@(\w+)\.group\(([^)]*)\)' # @xxx.group(...)
- r'(?:\s*@[\w.]+\([^)]*\))*' # optional additional decorators
- r'\s*def\s+(\w+)\([^)]*\)' # def xxx(...):
- r':\s*' # colon with optional whitespace
- r'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?' # optional docstring (""" or ''')
+ r'@(\w+)\.group\(([^)]*)\)' # @xxx.group(...)
+ + optional_decorator_pattern # optional additional decorators
+ + r'\s*def\s+(\w+)\([^)]*\)' # def xxx(...):
+ + r':\s*' # colon with optional whitespace
+ + r'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?' # optional docstring (""" or ''')
)
group_lookup = {}
@@ -271,11 +273,11 @@ 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'(?:\s*@[\w.]+\([^)]*\))*' # optional additional decorators
- r'\s*def\s+(\w+)\([^)]*\)' # def xxx(...):
- r':\s*' # colon with optional whitespace
- r'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?' # optional docstring (""" or ''')
+ r'@(\w+)\.command\(([^)]*)\)' # @xxx.command(...)
+ + optional_decorator_pattern # optional additional decorators
+ + r'\s*def\s+(\w+)\([^)]*\)' # def xxx(...):
+ + r':\s*' # colon with optional whitespace
+ + r'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?' # optional docstring (""" or ''')
)
for match in re.finditer(command_pattern, content):
diff --git a/cli-anything-plugin/tests/test_skill_generator.py b/cli-anything-plugin/tests/test_skill_generator.py
index 80d78c910..c389273b4 100644
--- a/cli-anything-plugin/tests/test_skill_generator.py
+++ b/cli-anything-plugin/tests/test_skill_generator.py
@@ -232,6 +232,56 @@ class TestExtractCliMetadata:
assert [cmd.name for cmd in groups["Configs"].commands] == ["show"]
assert [cmd.name for cmd in groups["Alerts Configs"].commands] == ["enable"]
+ def test_preserves_top_level_group_names_with_decorated_root(self, tmp_path):
+ software = "decorated"
+ 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()
+ @click.pass_context
+ def cli(ctx):
+ pass
+
+ @cli.group("devices")
+ def devices_group():
+ \"\"\"Device commands.\"\"\"
+ pass
+
+ @devices_group.command("list")
+ def list_devices():
+ \"\"\"List devices.\"\"\"
+ pass
+
+ @cli.group("alerts")
+ def alerts():
+ \"\"\"Alert commands.\"\"\"
+ pass
+
+ @alerts.group("configs")
+ def alerts_configs():
+ \"\"\"Alert config commands.\"\"\"
+ pass
+
+ @alerts_configs.command("enable")
+ def enable_alert_config():
+ \"\"\"Enable alert config.\"\"\"
+ pass
+ """)
+ )
+
+ metadata = extract_cli_metadata(str(tmp_path))
+ groups = {group.name: group for group in metadata.command_groups}
+
+ assert "Devices" in groups
+ assert "Cli Devices" not in groups
+ assert "Alerts Configs" in groups
+ assert [cmd.name for cmd in groups["Devices"].commands] == ["list"]
+ assert [cmd.name for cmd in groups["Alerts Configs"].commands] == ["enable"]
+
def test_generates_examples(self, harness_dir):
metadata = extract_cli_metadata(str(harness_dir))
assert len(metadata.examples) > 0