-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreadme-command-help.py
More file actions
65 lines (44 loc) · 1.55 KB
/
Copy pathreadme-command-help.py
File metadata and controls
65 lines (44 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import Optional
import subprocess
def generate_help_section(command: Optional[str], readme: str) -> str:
command_args = ["cargo", "run", "--"]
if command is not None:
command_args.append(command)
command_args.append("--help")
cmd_result = subprocess.run(command_args, capture_output=True)
cmd_result.check_returncode()
help_output = "\n".join(
[line.rstrip() for line in cmd_result.stdout.decode().splitlines()]
)
key = (command if command is not None else "base") + "-command-help"
start = f"<!-- START_SECTION:{key} -->"
end = f"<!-- END_SECTION:{key} -->"
lines: list[str] = []
inside = False
start_seen = False
end_seen = False
for line in readme.splitlines():
if inside:
if line == end:
lines.append(line)
inside = False
end_seen = True
elif line == start:
lines.append(line)
lines.append(f"\n```\n{help_output}\n```\n")
inside = True
start_seen = True
else:
lines.append(line)
if start_seen and end_seen:
return "\n".join(lines)
return readme
def read_readme() -> str:
with open("README.md", "r") as f:
return f.read()
if __name__ == "__main__":
content = read_readme()
for command in [None, "format", "check", "init", "completions"]:
content = generate_help_section(command, content)
with open("README.md", "w") as readme_file:
readme_file.write(content.strip() + "\n")