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

+1 -21
View File
@@ -31,27 +31,7 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string;
const service = new ApprovalService(repo);
let client: Awaited<ReturnType<typeof createSessionClient>> | undefined;
const positional: string[] = [];
for (let i = 0; i < rawArgs.length; i++) {
const arg = rawArgs[i];
if (arg === "--") {
positional.push(...rawArgs.slice(i + 1));
break;
}
if (arg.startsWith("-")) {
const eqIdx = arg.indexOf("=");
if (eqIdx === -1) {
const flagName = arg.replace(/^--?/, "").toLowerCase();
const isBool = ["raw", "json", "j", "help", "h", "version", "v", "yes", "y", "confirm", "apply", "execute"].includes(flagName);
if (!isBool && i + 1 < rawArgs.length && !rawArgs[i + 1].startsWith("-")) {
i++; // skip option value
}
}
continue;
}
positional.push(arg);
}
const positional = parsed.positional ?? [parsed.area, parsed.action, ...parsed.args].filter((v): v is string => Boolean(v));
const command = (positional[0] || "help").toLowerCase();
const jsonMode = booleanOption(parsed.options, ["json", "j"]);
const targetSn = positional[1] || stringOption(parsed.options, ["code", "sn", "id", "target"]);
+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;
}
+16
View File
@@ -1,5 +1,6 @@
import { describe, expect, it } from "bun:test";
import { runCli } from "./runner.js";
import { parseArgs, booleanOption } from "./parser.js";
import { VERSION_INFO, formatVersionLabel } from "../version.js";
describe("CLI runner - version and help aliases", () => {
@@ -98,4 +99,19 @@ describe("CLI runner - version and help aliases", () => {
expect(help.stderr).toBe("");
expect(version.stderr).toBe("");
});
it("does not swallow positional arguments after subcommand boolean flags", () => {
const parsed1 = parseArgs(["feetype", "get", "--all", "FT001"]);
expect(booleanOption(parsed1.options, ["all"])).toBe(true);
expect(parsed1.args).toEqual(["FT001"]);
const parsed2 = parseArgs(["workflow", "patch", "--in-place", "--value-json", "PIPE_123"]);
expect(booleanOption(parsed2.options, ["in-place"])).toBe(true);
expect(booleanOption(parsed2.options, ["value-json"])).toBe(true);
expect(parsed2.args).toEqual(["PIPE_123"]);
const parsed3 = parseArgs(["platform", "record", "toggle", "--active", "false", "REC_01"]);
expect(booleanOption(parsed3.options, ["active"], true)).toBe(false);
expect(parsed3.args).toEqual(["toggle", "REC_01"]);
});
});
+1
View File
@@ -5,6 +5,7 @@ export interface CliParsedArgs {
area?: string;
action?: string;
args: string[];
positional?: string[];
options: Record<string, string | boolean | number>;
raw: string[];
}