|
| 1 | +# (C) Datadog, Inc. 2020-present |
| 2 | +# All rights reserved |
| 3 | +# Licensed under the Apache license (see LICENSE) |
| 4 | +from typing import Iterator, List, Optional, cast |
| 5 | + |
| 6 | +import click |
| 7 | + |
| 8 | +from ._exceptions import MkDocsClickException |
| 9 | + |
| 10 | + |
| 11 | +def make_command_docs(prog_name: str, command: click.BaseCommand, level: int = 0) -> Iterator[str]: |
| 12 | + """Create the Markdown lines for a command and its sub-commands.""" |
| 13 | + for line in _recursively_make_command_docs(prog_name, command, level=level): |
| 14 | + yield line.replace("\b", "") |
| 15 | + |
| 16 | + |
| 17 | +def _recursively_make_command_docs( |
| 18 | + prog_name: str, command: click.BaseCommand, parent: click.Context = None, level: int = 0 |
| 19 | +) -> Iterator[str]: |
| 20 | + """Create the raw Markdown lines for a command and its sub-commands.""" |
| 21 | + ctx = click.Context(cast(click.Command, command), parent=parent) |
| 22 | + |
| 23 | + yield from _make_title(prog_name, level) |
| 24 | + yield from _make_description(ctx) |
| 25 | + yield from _make_usage(ctx) |
| 26 | + yield from _make_options(ctx) |
| 27 | + |
| 28 | + subcommands = _get_sub_commands(ctx.command, ctx) |
| 29 | + |
| 30 | + for command in sorted(subcommands, key=lambda cmd: cmd.name): |
| 31 | + yield from _recursively_make_command_docs(command.name, command, parent=ctx, level=level + 1) |
| 32 | + |
| 33 | + |
| 34 | +def _get_sub_commands(command: click.Command, ctx: click.Context) -> List[click.Command]: |
| 35 | + """Return subcommands of a Click command.""" |
| 36 | + subcommands = getattr(command, "commands", {}) |
| 37 | + if subcommands: |
| 38 | + return subcommands.values() |
| 39 | + |
| 40 | + if not isinstance(command, click.MultiCommand): |
| 41 | + return [] |
| 42 | + |
| 43 | + subcommands = [] |
| 44 | + |
| 45 | + for name in command.list_commands(ctx): |
| 46 | + subcommand = command.get_command(ctx, name) |
| 47 | + assert subcommand is not None |
| 48 | + subcommands.append(subcommand) |
| 49 | + |
| 50 | + return subcommands |
| 51 | + |
| 52 | + |
| 53 | +def _make_title(prog_name: str, level: int) -> Iterator[str]: |
| 54 | + """Create the first markdown lines describing a command.""" |
| 55 | + yield _make_header(prog_name, level) |
| 56 | + yield "" |
| 57 | + |
| 58 | + |
| 59 | +def _make_header(text: str, level: int) -> str: |
| 60 | + """Create a markdown header at a given level""" |
| 61 | + return f"{'#' * (level + 1)} {text}" |
| 62 | + |
| 63 | + |
| 64 | +def _make_description(ctx: click.Context) -> Iterator[str]: |
| 65 | + """Create markdown lines based on the command's own description.""" |
| 66 | + help_string = ctx.command.help or ctx.command.short_help |
| 67 | + |
| 68 | + if help_string: |
| 69 | + yield from help_string.splitlines() |
| 70 | + yield "" |
| 71 | + |
| 72 | + |
| 73 | +def _make_usage(ctx: click.Context) -> Iterator[str]: |
| 74 | + """Create the Markdown lines from the command usage string.""" |
| 75 | + |
| 76 | + # Gets the usual 'Usage' string without the prefix. |
| 77 | + formatter = ctx.make_formatter() |
| 78 | + pieces = ctx.command.collect_usage_pieces(ctx) |
| 79 | + formatter.write_usage(ctx.command_path, " ".join(pieces), prefix="") |
| 80 | + usage = formatter.getvalue().rstrip("\n") |
| 81 | + |
| 82 | + # Generate the full usage string based on parents if any, i.e. `root sub1 sub2 ...`. |
| 83 | + full_path = [] |
| 84 | + current: Optional[click.Context] = ctx |
| 85 | + while current is not None: |
| 86 | + name = current.command.name |
| 87 | + if name is None: |
| 88 | + raise MkDocsClickException(f"command {current.command} has no `name`") |
| 89 | + full_path.append(name) |
| 90 | + current = current.parent |
| 91 | + |
| 92 | + full_path.reverse() |
| 93 | + usage_snippet = " ".join(full_path) + usage |
| 94 | + |
| 95 | + yield "Usage:" |
| 96 | + yield "" |
| 97 | + yield "```" |
| 98 | + yield usage_snippet |
| 99 | + yield "```" |
| 100 | + yield "" |
| 101 | + |
| 102 | + |
| 103 | +def _make_options(ctx: click.Context) -> Iterator[str]: |
| 104 | + """Create the Markdown lines describing the options for the command.""" |
| 105 | + formatter = ctx.make_formatter() |
| 106 | + click.Command.format_options(ctx.command, ctx, formatter) |
| 107 | + # First line is redundant "Options" |
| 108 | + # Last line is `--help` |
| 109 | + option_lines = formatter.getvalue().splitlines()[1:-1] |
| 110 | + if not option_lines: |
| 111 | + return |
| 112 | + |
| 113 | + yield "Options:" |
| 114 | + yield "" |
| 115 | + yield "```" |
| 116 | + yield from option_lines |
| 117 | + yield "```" |
| 118 | + yield "" |
0 commit comments