Skip to content

Commit a05fb63

Browse files
committed
build(cli-docs): consolidate CLI generation script, md-click didn't work
1 parent c4ba813 commit a05fb63

File tree

3 files changed

+29
-51
lines changed

3 files changed

+29
-51
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ repos:
2626

2727
- repo: local
2828
hooks:
29-
- id: generate-cli-docs
30-
name: generate CLI documentation
31-
entry: python dev/generate_cli_docs.py
32-
language: system
33-
files: ^(python/cocoindex/cli\.py|dev/generate_cli_docs\.py)$
34-
pass_filenames: false
35-
3629
- id: maturin-develop
3730
name: maturin develop
3831
entry: maturin develop -E all,dev
3932
language: system
4033
files: ^(python/|src/|Cargo\.toml|pyproject\.toml)
4134
pass_filenames: false
4235

36+
- id: generate-cli-docs
37+
name: generate CLI documentation
38+
entry: python dev/generate_cli_docs.py
39+
language: system
40+
files: ^(python/cocoindex/cli\.py|dev/generate_cli_docs\.py)$
41+
pass_filenames: false
42+
4343
- id: cargo-fmt
4444
name: cargo fmt
4545
entry: cargo fmt

dev/generate_cli_docs.py

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@
77
"""
88

99
import sys
10-
import os
1110
from pathlib import Path
1211
import re
13-
from typing import Dict, List, Any
12+
import click
13+
from cocoindex.cli import cli
1414

1515
# Add the cocoindex python directory to the path
1616
project_root = Path(__file__).parent.parent
1717
python_path = project_root / "python"
1818
sys.path.insert(0, str(python_path))
1919

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-
2820

2921
def clean_usage_line(usage: str) -> str:
3022
"""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:
197189
return escape_html_tags(description) # Escape HTML tags for MDX compatibility
198190

199191

200-
def generate_command_docs(docs: List[Dict[str, Any]]) -> str:
192+
def generate_command_docs(cmd: click.Group) -> str:
201193
"""Generate markdown documentation for all commands."""
202194

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-
214195
markdown_content = []
215196

216197
# Add top-level heading to satisfy MD041 linting rule
217-
markdown_content.append("# CLI Commands")
198+
markdown_content.append("## Subcommands Reference")
218199
markdown_content.append("")
219200

201+
ctx = click.core.Context(cmd, info_name=cmd.name)
202+
subcommands = list(cmd.commands.values())
220203
# 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))
225209
description = extract_description(help_text)
226210

227-
markdown_content.append(f"## `{command_name}`")
211+
markdown_content.append(f"### `{command_name}`")
228212
markdown_content.append("")
229213

230214
if description:
@@ -252,19 +236,13 @@ def generate_command_docs(docs: List[Dict[str, Any]]) -> str:
252236
return "\n".join(markdown_content)
253237

254238

255-
def main():
239+
def main() -> None:
256240
"""Generate CLI documentation and save to file."""
257241
print("Generating CocoIndex CLI documentation...")
258242

259243
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-
266244
# Generate markdown content
267-
markdown_content = generate_command_docs(docs)
245+
markdown_content = generate_command_docs(cli)
268246

269247
# Determine output path
270248
docs_dir = project_root / "docs" / "docs" / "core"

docs/docs/core/cli-commands.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# CLI Commands
1+
## Subcommands Reference
22

3-
## `drop`
3+
### `drop`
44

55
Drop the backend setup for flows.
66

@@ -25,7 +25,7 @@ cocoindex drop [OPTIONS] [APP_TARGET] [FLOW_NAME]...
2525

2626
---
2727

28-
## `evaluate`
28+
### `evaluate`
2929

3030
Evaluate the flow and dump flow outputs to files.
3131

@@ -65,7 +65,7 @@ cocoindex evaluate [OPTIONS] APP_FLOW_SPECIFIER
6565

6666
---
6767

68-
## `ls`
68+
### `ls`
6969

7070
List all flows.
7171

@@ -91,7 +91,7 @@ cocoindex ls [OPTIONS] [APP_TARGET]
9191

9292
---
9393

94-
## `server`
94+
### `server`
9595

9696
Start a HTTP server providing REST APIs.
9797

@@ -123,7 +123,7 @@ cocoindex server [OPTIONS] APP_TARGET
123123

124124
---
125125

126-
## `setup`
126+
### `setup`
127127

128128
Check and apply backend setup changes for flows, including the internal
129129

@@ -146,7 +146,7 @@ cocoindex setup [OPTIONS] APP_TARGET
146146

147147
---
148148

149-
## `show`
149+
### `show`
150150

151151
Show the flow spec and schema.
152152

@@ -182,7 +182,7 @@ cocoindex show [OPTIONS] APP_FLOW_SPECIFIER
182182

183183
---
184184

185-
## `update`
185+
### `update`
186186

187187
Update the index to reflect the latest data from data sources.
188188

0 commit comments

Comments
 (0)