All files / src/commands help.ts

88.88% Statements 8/9
75% Branches 3/4
100% Functions 1/1
88.88% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67                                          10x                                                     14x     6x 6x 6x         8x 8x       8x      
/**
 * Help Command
 *
 * Provides help information for the CLI.
 * - `sentry help` or `sentry` (no args): Shows branded help with banner
 * - `sentry help <command>`: Shows detailed help for that command
 * - `sentry help --json`: Emits full command tree as structured JSON
 * - `sentry help --json <command>`: Emits specific command/group metadata as JSON
 */
 
import type { SentryContext } from "../context.js";
import { buildCommand } from "../lib/command.js";
import { OutputError } from "../lib/errors.js";
import { CommandOutput } from "../lib/formatters/output.js";
import {
  formatHelpHuman,
  introspectAllCommands,
  introspectCommand,
  printCustomHelp,
} from "../lib/help.js";
 
export const helpCommand = buildCommand({
  auth: false,
  docs: {
    brief: "Display help for a command",
    fullDescription:
      "Display help information. Run 'sentry help' for an overview, " +
      "or 'sentry help <command>' for detailed help on a specific command. " +
      "Use --json for machine-readable output suitable for AI agents.",
  },
  output: {
    human: formatHelpHuman,
    jsonExclude: ["_banner"] as const,
  },
  parameters: {
    flags: {},
    positional: {
      kind: "array",
      parameter: {
        brief: "Command to get help for",
        parse: String,
        placeholder: "command",
      },
    },
  },
  // biome-ignore lint/complexity/noBannedTypes: Stricli requires empty object for commands with no flags
  // biome-ignore lint/suspicious/useAwait: async generator required by Stricli buildCommand pattern
  async *func(this: SentryContext, _flags: {}, ...commandPath: string[]) {
    if (commandPath.length === 0) {
      // Yield the full command tree. Attach the branded banner for human display;
      // jsonExclude strips _banner from JSON output.
      const tree = introspectAllCommands();
      const banner = printCustomHelp();
      return yield new CommandOutput({ ...tree, _banner: banner });
    }
 
    // Resolve the command path and yield the result.
    // This ensures --json mode always gets structured output.
    const result = introspectCommand(commandPath);
    Iif ("error" in result) {
      // OutputError renders through the output system but exits non-zero
      throw new OutputError(result);
    }
    return yield new CommandOutput(result);
  },
});