From c60d49fb8c542b07b0514e3260d446f26aaabe88 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Tue, 29 Sep 2026 23:36:18 +0800 Subject: [PATCH] fix(cli): prevent boolean flags from swallowing positional args and unify aipro arg parsing --- src/aipro-cli.ts | 22 +-------- src/cli/parser.ts | 103 +++++++++++++++++++++++++++++++++++++++-- src/cli/runner.test.ts | 16 +++++++ src/cli/types.ts | 1 + 4 files changed, 117 insertions(+), 25 deletions(-) diff --git a/src/aipro-cli.ts b/src/aipro-cli.ts index 1b55a71..0c2e319 100644 --- a/src/aipro-cli.ts +++ b/src/aipro-cli.ts @@ -31,27 +31,7 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; const service = new ApprovalService(repo); let client: Awaited> | 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"]); diff --git a/src/cli/parser.ts b/src/cli/parser.ts index ff7770f..9480472 100644 --- a/src/cli/parser.ts +++ b/src/cli/parser.ts @@ -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 = {}; 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; } diff --git a/src/cli/runner.test.ts b/src/cli/runner.test.ts index dd273e5..9de4dfb 100644 --- a/src/cli/runner.test.ts +++ b/src/cli/runner.test.ts @@ -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"]); + }); }); diff --git a/src/cli/types.ts b/src/cli/types.ts index 0acad81..64269e0 100644 --- a/src/cli/types.ts +++ b/src/cli/types.ts @@ -5,6 +5,7 @@ export interface CliParsedArgs { area?: string; action?: string; args: string[]; + positional?: string[]; options: Record; raw: string[]; }