|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import sys |
10 | | -import os |
11 | 10 | from pathlib import Path |
12 | 11 | import re |
13 | | -from typing import Dict, List, Any |
| 12 | +import click |
| 13 | +from cocoindex.cli import cli |
14 | 14 |
|
15 | 15 | # Add the cocoindex python directory to the path |
16 | 16 | project_root = Path(__file__).parent.parent |
17 | 17 | python_path = project_root / "python" |
18 | 18 | sys.path.insert(0, str(python_path)) |
19 | 19 |
|
20 | | -try: |
21 | | - import md_click |
22 | | - from cocoindex.cli import cli |
23 | | -except ImportError as e: |
24 | | - print(f"Error importing required modules: {e}") |
25 | | - print("Make sure to run this script from the project root and install dependencies") |
26 | | - sys.exit(1) |
27 | | - |
28 | 20 |
|
29 | 21 | def clean_usage_line(usage: str) -> str: |
30 | 22 | """Clean up the usage line to remove 'cli' and make it generic, and remove the 'Usage:' prefix.""" |
@@ -197,34 +189,26 @@ def extract_description(help_text: str) -> str: |
197 | 189 | return escape_html_tags(description) # Escape HTML tags for MDX compatibility |
198 | 190 |
|
199 | 191 |
|
200 | | -def generate_command_docs(docs: List[Dict[str, Any]]) -> str: |
| 192 | +def generate_command_docs(cmd: click.Group) -> str: |
201 | 193 | """Generate markdown documentation for all commands.""" |
202 | 194 |
|
203 | | - # Separate main CLI from subcommands |
204 | | - main_cli = None |
205 | | - subcommands = [] |
206 | | - |
207 | | - for doc in docs: |
208 | | - parent = doc.get("parent", "") |
209 | | - if not parent: |
210 | | - main_cli = doc |
211 | | - else: |
212 | | - subcommands.append(doc) |
213 | | - |
214 | 195 | markdown_content = [] |
215 | 196 |
|
216 | 197 | # Add top-level heading to satisfy MD041 linting rule |
217 | | - markdown_content.append("# CLI Commands") |
| 198 | + markdown_content.append("## Subcommands Reference") |
218 | 199 | markdown_content.append("") |
219 | 200 |
|
| 201 | + ctx = click.core.Context(cmd, info_name=cmd.name) |
| 202 | + subcommands = list(cmd.commands.values()) |
220 | 203 | # Generate only the command details section (remove redundant headers) |
221 | | - for doc in sorted(subcommands, key=lambda x: x["command"].name): |
222 | | - command_name = doc["command"].name |
223 | | - help_text = doc["help"] |
224 | | - usage = clean_usage_line(doc["usage"]) |
| 204 | + for sub_cmd in sorted(subcommands, key=lambda x: x.name or ""): |
| 205 | + sub_ctx = click.core.Context(sub_cmd, info_name=sub_cmd.name, parent=ctx) |
| 206 | + command_name = sub_cmd.name |
| 207 | + help_text = sub_cmd.get_help(sub_ctx) |
| 208 | + usage = clean_usage_line(sub_cmd.get_usage(sub_ctx)) |
225 | 209 | description = extract_description(help_text) |
226 | 210 |
|
227 | | - markdown_content.append(f"## `{command_name}`") |
| 211 | + markdown_content.append(f"### `{command_name}`") |
228 | 212 | markdown_content.append("") |
229 | 213 |
|
230 | 214 | if description: |
@@ -252,19 +236,13 @@ def generate_command_docs(docs: List[Dict[str, Any]]) -> str: |
252 | 236 | return "\n".join(markdown_content) |
253 | 237 |
|
254 | 238 |
|
255 | | -def main(): |
| 239 | +def main() -> None: |
256 | 240 | """Generate CLI documentation and save to file.""" |
257 | 241 | print("Generating CocoIndex CLI documentation...") |
258 | 242 |
|
259 | 243 | try: |
260 | | - # Generate documentation using md-click |
261 | | - docs_generator = md_click.main.recursive_help(cli) |
262 | | - docs = list(docs_generator) |
263 | | - |
264 | | - print(f"Found {len(docs)} CLI commands to document") |
265 | | - |
266 | 244 | # Generate markdown content |
267 | | - markdown_content = generate_command_docs(docs) |
| 245 | + markdown_content = generate_command_docs(cli) |
268 | 246 |
|
269 | 247 | # Determine output path |
270 | 248 | docs_dir = project_root / "docs" / "docs" / "core" |
|
0 commit comments