|
| 1 | +// Facts Commander owns after parsing: the active path and which tokens became option values. |
| 2 | +import type { Command, Option } from "commander"; |
| 3 | + |
| 4 | +const activeErrorCommandByRoot = new WeakMap<Command, Command>(); |
| 5 | + |
| 6 | +function getCommandHierarchy(command: Command): Command[] { |
| 7 | + const hierarchy: Command[] = []; |
| 8 | + for (let current: Command | null = command; current; current = current.parent ?? null) { |
| 9 | + hierarchy.unshift(current); |
| 10 | + } |
| 11 | + return hierarchy; |
| 12 | +} |
| 13 | + |
| 14 | +function requiresFollowingValue(token: string, options: readonly Option[]): boolean { |
| 15 | + if (token.includes("=")) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + const exact = options.find((option) => option.short === token || option.long === token); |
| 19 | + if (exact) { |
| 20 | + return exact.required; |
| 21 | + } |
| 22 | + if (!token.startsWith("-") || token.startsWith("--")) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + const shortGroup = token.slice(1); |
| 26 | + for (let index = 0; index < shortGroup.length; index += 1) { |
| 27 | + const option = options.find((candidate) => candidate.short === `-${shortGroup[index]}`); |
| 28 | + if (!option) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + if (option.required || option.optional) { |
| 32 | + return option.required && index === shortGroup.length - 1; |
| 33 | + } |
| 34 | + } |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +/** Return whether Commander consumed one of the supplied argv tokens as a required option value. */ |
| 39 | +export function hasCommanderOptionValue( |
| 40 | + command: Command, |
| 41 | + argv: readonly string[], |
| 42 | + tokens: ReadonlySet<string>, |
| 43 | +): boolean { |
| 44 | + const hierarchy = getCommandHierarchy(command); |
| 45 | + const args = argv.slice(2); |
| 46 | + let commandIndex = 0; |
| 47 | + for (let index = 0; index < args.length; index += 1) { |
| 48 | + const arg = args[index]; |
| 49 | + if (arg === undefined || arg === "--") { |
| 50 | + break; |
| 51 | + } |
| 52 | + const nextCommand = hierarchy[commandIndex + 1]; |
| 53 | + if (nextCommand && (arg === nextCommand.name() || nextCommand.aliases().includes(arg))) { |
| 54 | + commandIndex += 1; |
| 55 | + continue; |
| 56 | + } |
| 57 | + // The root program enables positional options, so after dispatch Commander |
| 58 | + // parses only the active command's options; ancestor flags there never reach pre-action. |
| 59 | + if (requiresFollowingValue(arg, hierarchy[commandIndex]?.options ?? [])) { |
| 60 | + const value = args[index + 1]; |
| 61 | + if (value && tokens.has(value)) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + index += 1; |
| 65 | + } |
| 66 | + } |
| 67 | + return false; |
| 68 | +} |
| 69 | + |
| 70 | +/** Return the registered command path for the exact Commander node handling an error or action. */ |
| 71 | +export function getCommanderCommandPath(command: Command): string[] { |
| 72 | + const commandPath: string[] = []; |
| 73 | + for (let current: Command | null = command; current?.parent; current = current.parent) { |
| 74 | + commandPath.unshift(current.name()); |
| 75 | + } |
| 76 | + return commandPath; |
| 77 | +} |
| 78 | + |
| 79 | +function getRootCommand(command: Command): Command { |
| 80 | + let root = command; |
| 81 | + while (root.parent) { |
| 82 | + root = root.parent; |
| 83 | + } |
| 84 | + return root; |
| 85 | +} |
| 86 | + |
| 87 | +/** Scope the exact Commander node synchronously emitting an error. */ |
| 88 | +export function setCommanderErrorCommand(command: Command): () => void { |
| 89 | + const root = getRootCommand(command); |
| 90 | + const previous = activeErrorCommandByRoot.get(root); |
| 91 | + activeErrorCommandByRoot.set(root, command); |
| 92 | + return () => { |
| 93 | + if (previous) { |
| 94 | + activeErrorCommandByRoot.set(root, previous); |
| 95 | + } else { |
| 96 | + activeErrorCommandByRoot.delete(root); |
| 97 | + } |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +/** Return the active parse-error path without replacing Commander's configured output handler. */ |
| 102 | +export function getCommanderErrorCommandPath(program: Command): string[] | undefined { |
| 103 | + const command = activeErrorCommandByRoot.get(getRootCommand(program)); |
| 104 | + return command ? getCommanderCommandPath(command) : undefined; |
| 105 | +} |
0 commit comments