fix(cli): report argv parse failures as PARSE_ERROR

Unknown flags, missing flag values and surplus positional arguments
reported the catch-all UNKNOWN code, while a misspelled flag already
reported PARSE_ERROR. Every argv parse failure now reports PARSE_ERROR
with retryable false; UNKNOWN stays reserved for unexpected failures.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
gensokyo-devandgensokyo-dev authored and Ysopwnable committed 2026-11-28 15:30:06 +08:00
1 parent 30be558254
commit c5cbb212d7
11 files changed
+41 -24

No files matched your search

+4 -3
View File
@@ -49,16 +49,17 @@ export const cliArgumentFailure = (
command: string,
failure: CliArgvFailure,
): CliCommandFailure => {
if (failure.kind === "dry_run" || failure.kind === "flag_spelling")
return { code: "PARSE_ERROR", message: failure.message, retryable: false };
// Unknown flags, missing values and surplus positionals are argv parse
// failures like a misspelled flag; UNKNOWN stays reserved for unexpected errors.
if (failure.kind !== "validation")
return {
code: "UNKNOWN",
code: "PARSE_ERROR",
message:
failure.message +
(failure.kind === "surplus"
? ` Use '${cli} ${command} --help' for the declared argument shape.`
: ""),
retryable: false,
};
return {
code: "VALIDATION_ERROR",
+5 -1
View File
@@ -217,7 +217,11 @@ for (const full of [false, true]) {
}),
],
}
: { code: "UNKNOWN", message: "Unknown flag: --zzz-bogus" };
: {
code: "PARSE_ERROR",
message: "Unknown flag: --zzz-bogus",
retryable: false,
};
expect(JSON.parse(invalid.stdout)).toMatchObject({
$rea: "jsonl/v1",
type: "error",
@@ -237,8 +237,9 @@ describe("lazy CLI execution", () => {
expect(result.stderr, name).not.toContain(COMPLETE_CLI_IMPORT);
const diagnostic: unknown = JSON.parse(result.stdout);
expect(diagnostic, name).toMatchObject({
code: "UNKNOWN",
code: "PARSE_ERROR",
message: expect.stringContaining("--rea-test-unknown-option"),
retryable: false,
});
diagnostics.set(name, diagnostic);
}),
@@ -45,7 +45,6 @@ describe("CLI --sensitive-value parser diagnostics", () => {
// The parser refuses the schema key spelling; its value stays redacted.
message:
"Unknown flag: --sensitiveValue. Did you mean --sensitive-value?",
failure: { code: "PARSE_ERROR", retryable: false },
},
{
// Incur removes builtin output flags before reading the declared value.
@@ -62,14 +61,15 @@ describe("CLI --sensitive-value parser diagnostics", () => {
},
])(
"excludes declarations from a JSON $name rejection",
async (
{ arguments: arguments_, message, failure = { code: "UNKNOWN" } },
{ cli },
) => {
async ({ arguments: arguments_, message }, { cli }) => {
const result = await cli.run({ arguments: [...arguments_, "--json"] });
expectExcluded(result);
expect(JSON.parse(result.stdout)).toEqual({ ...failure, message });
expect(JSON.parse(result.stdout)).toEqual({
code: "PARSE_ERROR",
message,
retryable: false,
});
},
);
@@ -80,7 +80,7 @@ describe("CLI --sensitive-value parser diagnostics", () => {
arguments: [
...CAPTURE,
"har",
...["flag", "code", "UNKNOWN"].flatMap((literal) => [
...["flag", "code", "PARSE_ERROR"].flatMap((literal) => [
"--sensitive-value",
literal,
]),
@@ -91,8 +91,9 @@ describe("CLI --sensitive-value parser diagnostics", () => {
expect(result.exitCode).toBe(1);
expect(JSON.parse(result.stdout)).toEqual({
code: "UNKNOWN",
code: "PARSE_ERROR",
message: "Unknown [REDACTED]: --no-x",
retryable: false,
});
},
);
@@ -129,8 +130,9 @@ describe("CLI --sensitive-value parser diagnostics", () => {
});
expect(undeclared.exitCode).toBe(1);
expect(JSON.parse(undeclared.stdout)).toEqual({
code: "UNKNOWN",
code: "PARSE_ERROR",
message: `Unknown flag: --no-${LITERAL}`,
retryable: false,
});
// An accepted output value is honored, so the service failure is JSON.
+8 -4
View File
@@ -506,10 +506,14 @@ describe("Command-owned JSON Lines parser diagnostics", () => {
});
await stream.output.finish();
expect(status).toBe(1);
const diagnostic = {
code: extra.length === 0 ? "VALIDATION_ERROR" : "UNKNOWN",
message: expect.any(String),
};
const diagnostic =
extra.length === 0
? { code: "VALIDATION_ERROR", message: expect.any(String) }
: {
code: "PARSE_ERROR",
message: expect.any(String),
retryable: false,
};
expect(lines(stream.text())).toEqual([
expect.objectContaining({
$rea: "jsonl/v1",
+2 -1
View File
@@ -1,8 +1,9 @@
$ rea inspect-byte-entropy <workspace>/sample.bin extra
exit: 1
--- stdout
code: UNKNOWN
code: PARSE_ERROR
message: "Unexpected positional arguments: \"extra\". Use 'rea inspect-byte-entropy --help' for the declared argument shape."
retryable: false
<end>
--- stderr
<end>
+2 -1
View File
@@ -1,8 +1,9 @@
$ rea inspect-byte-entropy <workspace>/sample.bin --block-size
exit: 1
--- stdout
code: UNKNOWN
code: PARSE_ERROR
message: "Missing value for flag: --block-size"
retryable: false
<end>
--- stderr
<end>
+2 -1
View File
@@ -1,8 +1,9 @@
$ rea inspect-web-network-capture <workspace>/capture.har har secret-literal --sensitive-value secret-literal
exit: 1
--- stdout
code: UNKNOWN
code: PARSE_ERROR
message: "Unexpected positional arguments: \"[REDACTED]\". Use 'rea inspect-web-network-capture --help' for the declared argument shape."
retryable: false
<end>
--- stderr
<end>
+2 -1
View File
@@ -1,8 +1,9 @@
$ rea inspect-byte-entropy <workspace>/sample.bin -- --block-size
exit: 1
--- stdout
code: UNKNOWN
code: PARSE_ERROR
message: "Unknown flag: --"
retryable: false
<end>
--- stderr
<end>
+2 -1
View File
@@ -1,8 +1,9 @@
$ rea inspect-byte-entropy <workspace>/sample.bin --no-such-flag
exit: 1
--- stdout
code: UNKNOWN
code: PARSE_ERROR
message: "Unknown flag: --no-such-flag"
retryable: false
<end>
--- stderr
<end>
+1 -1
View File
@@ -1,7 +1,7 @@
$ rea inspect-byte-entropy <workspace>/sample.bin --no-such-flag # stdout is a terminal
exit: 1
--- stdout
Error: Unknown flag: --no-such-flag
Error (PARSE_ERROR): Unknown flag: --no-such-flag
<end>
--- stderr
<end>