import type { CommandDef } from "citty"; type LazyCommandDef = CommandDef | (() => CommandDef | Promise); export interface ResolvedCommandUsage { command: CommandDef; parent?: CommandDef; } async function loadSubcommand(command: CommandDef, name: string): Promise { const subcommands = command.subCommands as Record | undefined; const candidate = subcommands?.[name]; if (!candidate) return undefined; return typeof candidate === "function" ? candidate() : candidate; } /** Resolve the deepest adjacent citty subcommand named by argv. */ export async function resolveCommandUsage( root: CommandDef, argv: readonly string[], ): Promise { let command = root; let parent: CommandDef | undefined; for (const argument of argv) { if (argument.startsWith("-")) break; const child = await loadSubcommand(command, argument); if (!child) break; parent = command; command = child; } return { command, parent }; }