Files
open-design/tools/dev/src/cli-args.ts
T
87d03692a1 [codex] Load tools-dev env files (#4430)
* Load tools-dev env files

* fix(tools-dev): preserve global help fallback

Generated-By: looper 0.9.10 (runner=fixer, agent=codex)

* Make tools-dev env files override shell

* Fix tools-dev env-file option parsing

Generated-By: looper 0.9.10+codex.autoclean (runner=fixer, agent=codex)

---------

Co-authored-by: Siri-Ray <2667192167@qq.com>
Co-authored-by: Looper <looper@noreply.github.com>
2026-06-17 12:26:00 +08:00

43 lines
1.4 KiB
TypeScript

const CLI_COMMANDS = new Set(["start", "run", "status", "stop", "restart", "logs", "inspect", "check", "help"]);
const OPTIONS_WITH_VALUE = new Set([
"--daemon-port",
"--env-file",
"--expr",
"--namespace",
"--path",
"--selector",
"--timeout",
"--tools-dev-root",
"--update-action",
"--web-port",
]);
export function rewriteCliArgsForDefaultStart(args: readonly string[]): string[] {
const firstPositionalIndex = firstPositionalArgIndex(args);
const firstPositional = firstPositionalIndex >= 0 ? args[firstPositionalIndex] : undefined;
if (isGlobalHelpRequest(args, firstPositional) || CLI_COMMANDS.has(firstPositional ?? "")) return [...args];
const rewritten = [...args];
rewritten.splice(firstPositionalIndex >= 0 ? firstPositionalIndex : 0, 0, "start");
return rewritten;
}
function isGlobalHelpRequest(args: readonly string[], firstPositional: string | undefined): boolean {
return firstPositional === undefined && args.some((arg) => arg === "--help" || arg === "-h");
}
function firstPositionalArgIndex(args: readonly string[]): number {
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--") return index + 1 < args.length ? index + 1 : -1;
if (arg.startsWith("--") && arg.includes("=")) continue;
if (OPTIONS_WITH_VALUE.has(arg)) {
index += 1;
continue;
}
if (arg.startsWith("-")) continue;
return index;
}
return -1;
}