fix(cli): prevent boolean flags from swallowing positional args and unify aipro arg parsing

This commit is contained in:
gitea committed 2026-09-29 23:36:18 +08:00
1 parent d0af46b445
commit c60d49fb8c
4 files changed
+117 -25

No files matched your search

+99 -4
View File
@@ -17,6 +17,7 @@ const KNOWN_BOOLEAN_FLAGS = new Set([
"check-only",
"changelog",
"log",
"l",
"local",
"workspace",
"external",
@@ -25,13 +26,87 @@ const KNOWN_BOOLEAN_FLAGS = new Set([
"allowFull",
"summary",
"yes",
"y",
"confirm",
"force-write",
"forceWrite",
"dry-run",
"dryRun",
"dryrun",
"all",
"a",
"include-disabled",
"flat",
"flatten",
"table",
"specs",
"spec",
"fields",
"recursive",
"in-place",
"inPlace",
"value-json",
"valueJson",
"has-error",
"hasError",
"errors",
"failed",
"fail",
"fail-on-alert",
"failOnAlert",
"includeDetail",
"detail",
"apply",
"execute",
"do",
"refresh",
"overwrite",
"elevate",
"elevate-hose",
"as-hose",
"no-elevate",
"reveal",
"plain",
"save",
"persist",
"verify",
"workbench",
"w",
"active-only",
"activeOnly",
"active",
"read",
"unread",
"clear",
"sms",
"sync",
"interactive",
"i",
"reminder",
"remind",
"keep-it-secret",
"secret",
"private",
"mine",
"my",
"self",
"distinct",
"risk",
"with-risk",
"audit",
"github",
"gh",
"gitea",
"yingqing",
"mirror",
]);
function isExplicitBooleanLiteral(val: string | undefined): boolean {
if (!val) return false;
const lower = val.toLowerCase();
return lower === "true" || lower === "false";
}
export function parseArgs(rawArgs: string[]): CliParsedArgs {
const options: Record<string, string | boolean | number> = {};
const positional: string[] = [];
@@ -53,11 +128,18 @@ export function parseArgs(rawArgs: string[]): CliParsedArgs {
} else {
const key = arg.slice(2);
const keyLower = key.toLowerCase();
const next = rawArgs[i + 1];
if (KNOWN_BOOLEAN_FLAGS.has(key) || KNOWN_BOOLEAN_FLAGS.has(keyLower)) {
options[key] = true;
if (key !== keyLower) options[keyLower] = true;
if (isExplicitBooleanLiteral(next)) {
const boolVal = next!.toLowerCase() === "true";
options[key] = boolVal;
if (key !== keyLower) options[keyLower] = boolVal;
i++;
} else {
options[key] = true;
if (key !== keyLower) options[keyLower] = true;
}
} else {
const next = rawArgs[i + 1];
if (next && !next.startsWith("-")) {
options[key] = next;
i++;
@@ -86,6 +168,14 @@ export function parseArgs(rawArgs: string[]): CliParsedArgs {
} else {
options.o = true;
}
} else if (flag === "c") {
const next = rawArgs[i + 1];
if (next && !next.startsWith("-")) {
options.c = next;
i++;
} else {
options.c = true;
}
} else {
options[flag] = true;
}
@@ -103,6 +193,7 @@ export function parseArgs(rawArgs: string[]): CliParsedArgs {
area,
action,
args,
positional,
options,
raw: rawArgs,
};
@@ -128,7 +219,11 @@ export function booleanOption(
for (const k of keys) {
const val = options[k];
if (typeof val === "boolean") return val;
if (typeof val === "string") return val === "true" || val === "1";
if (typeof val === "string") {
const lower = val.toLowerCase();
if (lower === "true" || lower === "1") return true;
if (lower === "false" || lower === "0") return false;
}
}
return fallback;
}