-
Notifications
You must be signed in to change notification settings - Fork 0
fix: canonicalize legacy cli crawler command surface #23
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
Merged
nsalvacao
merged 2 commits into
candidate/lote2-audit-2026-03-16
from
worker/lote2-aud007-command-surface
Mar 16, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,103 +1,12 @@ | ||
| #!/usr/bin/env python3 | ||
| """Universal CLI Help Crawler - OpenAPI for CLIs. | ||
| """Legacy compatibility script for ``cli-crawler``. | ||
|
|
||
| Crawls CLI --help outputs and generates structured JSON maps | ||
| that AI agents can use for precise command reasoning. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import logging | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from crawler.config import CLIConfig, CrawlerConfig, load_config | ||
| from crawler.pipeline import crawl_all, crawl_cli | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="Crawl CLI --help outputs and generate structured JSON maps", | ||
| epilog="Examples:\n" | ||
| " python cli_crawler.py git -o output/git.json\n" | ||
| " python cli_crawler.py --config config.yaml --all\n" | ||
| " python cli_crawler.py docker -v --include-raw\n", | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument("cli", nargs="?", help="CLI to crawl (e.g., git, docker)") | ||
| parser.add_argument("--config", "-c", type=Path, help="Path to config YAML") | ||
| parser.add_argument("--output", "-o", type=Path, help="Output file path") | ||
| parser.add_argument( | ||
| "--output-dir", | ||
| type=Path, | ||
| default=Path("./output"), | ||
| help="Output directory (default: ./output)", | ||
| ) | ||
| parser.add_argument("--all", action="store_true", help="Crawl all CLIs in config") | ||
| parser.add_argument( | ||
| "--include-raw", action="store_true", help="Include raw help text in main JSON" | ||
| ) | ||
| parser.add_argument("--verbose", "-v", action="store_true", help="Verbose logging") | ||
| parser.add_argument("--strict", action="store_true", help="Fail on first parse error") | ||
| parser.add_argument("--max-depth", type=int, help="Override max recursion depth") | ||
| parser.add_argument("--timeout", type=int, help="Override timeout per command (seconds)") | ||
| parser.add_argument("--list", action="store_true", help="List configured CLIs and exit") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Configure logging | ||
| logging.basicConfig( | ||
| level=logging.DEBUG if args.verbose else logging.INFO, | ||
| format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", | ||
| datefmt="%H:%M:%S", | ||
| ) | ||
| Prefer invoking the canonical command directly: | ||
|
|
||
| # Load config | ||
| config: CrawlerConfig | ||
| if args.config and args.config.exists(): | ||
| config = load_config(str(args.config)) | ||
| else: | ||
| config = CrawlerConfig() | ||
|
|
||
| # List mode | ||
| if args.list: | ||
| if not config.clis: | ||
| print("No CLIs configured. Use --config to specify a config file.") | ||
| else: | ||
| print(f"Configured CLIs ({len(config.clis)}):") | ||
| for name, cfg in sorted(config.clis.items()): | ||
| group = f" [{cfg.group}]" if cfg.group else "" | ||
| env = f" (env: {cfg.environment})" if cfg.environment != "wsl" else "" | ||
| print(f" {name}{group}{env}") | ||
| return | ||
|
|
||
| # Crawl all CLIs | ||
| if args.all: | ||
| if not config.clis: | ||
| print("No CLIs configured. Use --config to specify a config file.") | ||
| sys.exit(1) | ||
| crawl_all(config, args.output_dir, args.include_raw, args.strict) | ||
| return | ||
|
|
||
| # Crawl single CLI | ||
| if args.cli: | ||
| cli_config = config.clis.get(args.cli, CLIConfig(name=args.cli)) | ||
|
|
||
| # Apply CLI arg overrides | ||
| if args.max_depth is not None: | ||
| cli_config.max_depth = args.max_depth | ||
| if args.timeout is not None: | ||
| cli_config.timeout = args.timeout | ||
|
|
||
| output = args.output or args.output_dir / f"{args.cli}.json" | ||
| crawl_cli(args.cli, cli_config, output, args.include_raw, args.strict) | ||
| return | ||
|
|
||
| # No action specified | ||
| parser.print_help() | ||
| sys.exit(1) | ||
| cli-crawler <cli_name> [options] | ||
| """ | ||
|
|
||
| from crawler.cli_crawler import main | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Compatibility tests for legacy ``cli_crawler`` wrappers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from crawler import cli_crawler | ||
|
|
||
|
|
||
| def test_main_maps_include_raw_to_raw( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """Legacy ``--include-raw`` should map to canonical ``--raw``.""" | ||
| monkeypatch.setattr(sys, "argv", ["cli_crawler.py", "git", "--include-raw"]) | ||
| captured_argv: list[str] = [] | ||
|
|
||
| def _fake_pipeline_main() -> None: | ||
| captured_argv.extend(sys.argv) | ||
|
|
||
| monkeypatch.setattr(cli_crawler._pipeline, "main", _fake_pipeline_main) | ||
|
|
||
| cli_crawler.main() | ||
|
|
||
| assert captured_argv == ["cli_crawler.py", "git", "--raw"] | ||
| stderr = capsys.readouterr().err | ||
| assert "cli-crawler" in stderr | ||
| assert "--include-raw is deprecated; treating it as --raw." in stderr | ||
|
|
||
|
|
||
| def test_main_prefers_raw_when_both_raw_flags_are_present( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """When both flags are present, keep one canonical ``--raw`` only.""" | ||
| monkeypatch.setattr( | ||
| sys, | ||
| "argv", | ||
| ["cli_crawler.py", "git", "--raw", "--include-raw"], | ||
| ) | ||
| captured_argv: list[str] = [] | ||
|
|
||
| def _fake_pipeline_main() -> None: | ||
| captured_argv.extend(sys.argv) | ||
|
|
||
| monkeypatch.setattr(cli_crawler._pipeline, "main", _fake_pipeline_main) | ||
|
|
||
| cli_crawler.main() | ||
|
|
||
| assert captured_argv == ["cli_crawler.py", "git", "--raw"] | ||
| stderr = capsys.readouterr().err | ||
| assert "--include-raw is deprecated and ignored when --raw is also provided." in stderr | ||
nsalvacao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.