refactor(cli): rely on Commander contracts (#106366)

This commit is contained in:
Peter Steinberger
2026-07-13 05:05:31 -07:00
committed by GitHub
parent a03df5fd58
commit 801ec63f92
13 changed files with 42 additions and 155 deletions
+3 -13
View File
@@ -2,19 +2,9 @@
import type { Command } from "commander";
export function hasExplicitOptions(command: Command, names: readonly string[]): boolean {
if (typeof command.getOptionValueSource !== "function") {
return false;
}
return names.some((name) => command.getOptionValueSource(name) === "cli");
}
function getOptionSource(command: Command, name: string): string | undefined {
if (typeof command.getOptionValueSource !== "function") {
return undefined;
}
return command.getOptionValueSource(name);
}
// Defensive guardrail: allow expected parent/grandparent inheritance without unbounded deep traversal.
const MAX_INHERIT_DEPTH = 2;
@@ -27,7 +17,7 @@ export function inheritOptionFromParent<T = unknown>(
return undefined;
}
const childSource = getOptionSource(command, name);
const childSource = command.getOptionValueSource(name);
if (childSource && childSource !== "default") {
return undefined;
}
@@ -35,9 +25,9 @@ export function inheritOptionFromParent<T = unknown>(
let depth = 0;
let ancestor = command.parent;
while (ancestor && depth < MAX_INHERIT_DEPTH) {
const source = getOptionSource(ancestor, name);
const source = ancestor.getOptionValueSource(name);
if (source && source !== "default") {
return ancestor.opts<Record<string, unknown>>()[name] as T | undefined;
return ancestor.getOptionValue(name) as T | undefined;
}
depth += 1;
ancestor = ancestor.parent;