Merge pull request #233 from DeryFerd/fix/cli-arg-errors-before-early-exit

fix(cli): validate flags before the no-command and mcp early returns
This commit is contained in:
Timo Zhang
2026-11-17 11:45:59 +08:00
committed by GitHub
2 changed files with 49 additions and 8 deletions
+25 -7
View File
@@ -546,12 +546,10 @@ export async function runCli(argv: readonly string[] = process.argv.slice(2)): P
return 0;
}
if (parsed.command === "mcp") {
startMcpStdioServer();
return 0;
}
if (parsed.help || !parsed.command) {
// `--help` and `--version` short-circuit on purpose, but a bad flag must not
// be swallowed just because no subcommand was given: `bla --bogus` used to
// print help and exit 0, so a CI typo looked like success.
if (parsed.help) {
printHelp();
return 0;
}
@@ -561,6 +559,16 @@ export async function runCli(argv: readonly string[] = process.argv.slice(2)): P
return 1;
}
if (parsed.command === "mcp") {
startMcpStdioServer();
return 0;
}
if (!parsed.command) {
printHelp();
return 0;
}
try {
switch (parsed.command) {
case "setup": {
@@ -1272,9 +1280,19 @@ const isDirectExecution =
process.argv[1].endsWith("bla-agents") ||
process.argv[1].endsWith("bla-shield"));
/**
* The stdio server owns the process lifetime once it starts, so a zero exit
* from the `mcp` path must not be written to `process.exitCode`. A non-zero
* code means the server never started (bad flags), and that must still surface.
*/
export function shouldSetProcessExitCode(argv: readonly string[], code: number): boolean {
const stdioServerPath = argv[2] === "mcp" || argv.includes("--mcp");
return !stdioServerPath || code !== 0;
}
if (isDirectExecution) {
runCli().then((code) => {
if (process.argv[2] !== "mcp" && !process.argv.includes("--mcp")) {
if (shouldSetProcessExitCode(process.argv, code)) {
process.exitCode = code;
}
});
+24 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { parseCliArgs, runCli } from "../src/cli.js";
import { parseCliArgs, runCli, shouldSetProcessExitCode } from "../src/cli.js";
describe("CLI flag validation", () => {
it("rejects out-of-range --fail-under values", () => {
@@ -38,6 +38,29 @@ describe("CLI flag validation", () => {
await expect(runCli(["--help", "--fail-under", "-1"])).resolves.toBe(0);
});
it("returns exit code 1 for invalid flags even when no subcommand is given", async () => {
await expect(runCli(["--bogus"])).resolves.toBe(1);
await expect(runCli(["--fail-under", "-1"])).resolves.toBe(1);
await expect(runCli([])).resolves.toBe(0);
});
it("rejects invalid flags passed to mcp before starting the server", async () => {
await expect(runCli(["mcp", "--bogus"])).resolves.toBe(1);
});
it("still surfaces a non-zero exit code on the mcp path", () => {
// The stdio server owns the process lifetime once it starts, so a zero exit
// is suppressed there — but a refused invocation must not be.
expect(shouldSetProcessExitCode(["node", "cli.js", "mcp"], 0)).toBe(false);
expect(shouldSetProcessExitCode(["node", "cli.js", "mcp", "--bogus"], 1)).toBe(true);
expect(shouldSetProcessExitCode(["node", "cli.js", "audit", "."], 1)).toBe(true);
expect(shouldSetProcessExitCode(["node", "cli.js", "audit", "."], 0)).toBe(true);
});
it("keeps --version short-circuiting ahead of flag validation", async () => {
await expect(runCli(["--version", "--bogus"])).resolves.toBe(0);
});
it("accepts --flag=value spellings for every value flag", () => {
const parsed = parseCliArgs([
"defend",