#!/usr/bin/env python3 """ Auto-generates documentation from command defs in console.py. """ import click import html import os import re import shutil import textwrap from click import Command from twitchdl.cli import cli START_MARKER = "" END_MARKER = "" def main(): update_changelog() parent_ctx = click.Context(cli, info_name="twitch-dl") for name, command in cli.commands.items(): ctx = click.Context(cli, info_name=name, parent=parent_ctx) update_docs(command, ctx) def update_changelog(): print("Updating: docs/changelog.md") root = os.path.realpath(os.path.dirname(os.path.dirname(__file__))) source = os.path.join(root, "CHANGELOG.md") target = os.path.join(root, "docs/changelog.md") shutil.copy(source, target) def update_docs(command: Command, ctx: click.Context): path = os.path.join("docs", "commands", f"{command.name}.md") content = render_command(command, ctx) if not os.path.exists(path): print(f"Creating: {path}") write(path, content) else: print(f"Updating: {path}") [_, handwritten] = read(path).split(END_MARKER) content = f"{content.strip()}\n\n{END_MARKER}\n\n{handwritten.strip()}" write(path, content) def render_command(command: Command, ctx: click.Context): content = START_MARKER content += f"\n# twitch-dl {command.name}\n\n" if command.help: content += command.help + "\n\n" content += render_usage(ctx, command) content += render_options(ctx, command) return content def render_usage(ctx: click.Context, command: Command): content = "### USAGE\n\n" content += "```\n" content += command.get_usage(ctx).replace("Usage: ", "") content += "\n```\n\n" return content def render_options(ctx, command: Command): options = list(get_options(command)) if not options: return "" content = "### OPTIONS\n\n" content += "
{escape(opts)} | {escape(help)} |
\\1
", text)
return text
if __name__ == "__main__":
main()