usageOf

tcmts.cli.help.usageOf · #4qnfxi4j · term · runs on the server

uses tcmts.cli.style.bold, which uses node:util

What a command takes, as its help says it: its usage line (its subcommands'), arguments, subcommands and options. Help shows it, and so does a usage error.

import { argLabel, childrenOf, described, lookup, optionLabel, paramsOf, rows } from "#codebase/tcmts/cli/help";
import { bold } from "#codebase/tcmts/cli/style";

export function usageOf(path: string[]): string[] {
  const r = lookup(path);
  const params = paramsOf(r);
  const args = params.filter((p) => p.cli.syntax!.type === "argument");
  const options = params.filter((p) => p.cli.syntax!.type === "option");
  const children = childrenOf(r);
  const usage = [
    `tcm ${path.join(" ")}`,
    ...args.map(argLabel),
    ...(options.length ? ["[options]"] : []),
  ].join(" ");
  const out = [bold("Usage"), `  ${usage}`];
  for (const c of children)
    out.push(
      `  tcm ${path.join(" ")} ${c.name}${
        paramsOf(c).some((p) => p.cli.syntax!.type === "argument")
          ? " " +
            paramsOf(c)
              .filter((p) => p.cli.syntax!.type === "argument")
              .map(argLabel)
              .join(" ")
          : ""
      }`,
    );
  if (args.length)
    out.push("", bold("Arguments"), ...rows(args.map((p) => [p.name, described(p)])));
  if (children.length)
    out.push("", bold("Commands"), ...rows(children.map((c) => [c.name, c.description ?? ""])));
  out.push(
    "",
    bold("Options"),
    ...rows([
      ...options.map((p): [string, string] => [optionLabel(p), described(p)]),
      ["-h, --help", "show this help"],
    ]),
  );
  return out;
}

uses

used by