Skip to content

Instantly share code, notes, and snippets.

@sa-
Last active October 25, 2024 16:34
Show Gist options
  • Save sa-/47d74b100705b891d08bc7cb08a26de7 to your computer and use it in GitHub Desktop.
Save sa-/47d74b100705b891d08bc7cb08a26de7 to your computer and use it in GitHub Desktop.
Compiles helptexts for all commands and subcommands
"""
Add this to pre-commit config
```
- repo: local
hooks:
- id: helptext-compiler
name: helptext-compiler
description: "Generates help texts for the CLI"
language: system
entry: poetry run python path/to/this/file/helptext_compiler.py
```
"""
from click.testing import CliRunner
from WHEREVER import cli
def collect_help_texts(command, parent_commands=None):
"""Recursively collect help texts for a command and its subcommands."""
if parent_commands is None:
parent_commands = []
help_texts = {}
# Create the full command path
command_path = " ".join(parent_commands + [command.name]) if command.name else ""
# Get help text for current command
runner = CliRunner()
result = runner.invoke(command, ["--help"])
help_texts[command_path] = result.output
# Recursively get help text for subcommands
if hasattr(command, "commands"):
for subcommand in command.commands.values():
help_texts.update(collect_help_texts(subcommand, parent_commands + [command.name] if command.name else []))
return help_texts
def generate_help_documentation(cli, output_file="help_documentation.md"):
"""Generate help documentation for all commands and save to a markdown file."""
help_texts = collect_help_texts(cli)
with open(output_file, "w") as f:
f.write("# Command Line Interface Documentation\n\n")
for command_path, help_text in sorted(help_texts.items()):
if command_path:
f.write(f"\n## {command_path}\n\n")
else:
f.write("## zml\n\n")
f.write("```\n")
f.write(help_text)
f.write("\n```\n")
if __name__ == "__main__":
generate_help_documentation(cli, "docs/cli_help.md")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment