Add flag metadata to the argv parser: reject unknown flags, fix boolean/value ambiguity
parseArgs decided a flag's kind from whatever token followed it, so `corps list --all now` fed "now" into --all and crashed with Invalid boolean, a bare `--limit` was silently dropped (full dataset pull), and `--limitt` was silently ignored with exit 0. Add BOOLEAN_FLAGS / VALUE_FLAGS / KNOWN_FLAGS registries derived from how the code reads each flag (booleanOption vs stringOption/numberOption). Booleans never consume the next token, value flags must receive one, unknown flags fail with a did-you-mean suggestion, and the three dual-use flags (f, h, version) keep the legacy heuristic. parser.test.ts — previously zero tests on the entry point — locks all of it plus a help.ts cross-check.
This commit is contained in:
1 parent
935ca015b1
commit
68fd90dbda
2 files changed
+339
No files matched your search
@@ -0,0 +1,155 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
booleanOption,
|
||||
BOOLEAN_FLAGS,
|
||||
KNOWN_FLAGS,
|
||||
numberOption,
|
||||
parseArgs,
|
||||
splitOption,
|
||||
stringOption,
|
||||
VALUE_FLAGS
|
||||
} from "./parser.js";
|
||||
|
||||
describe("parseArgs flag handling", () => {
|
||||
test("boolean flags never consume the next token", () => {
|
||||
// Before the metadata registry, `--all now` fed "now" into --all and
|
||||
// booleanOption("now") threw `Invalid boolean: now`.
|
||||
const parsed = parseArgs(["corps", "list", "--all", "now"]);
|
||||
|
||||
expect(parsed.options.all).toBe(true);
|
||||
expect(parsed.command).toEqual(["corps", "list", "now"]);
|
||||
});
|
||||
|
||||
test("boolean flags at the end of argv stay boolean", () => {
|
||||
expect(parseArgs(["corps", "list", "--all"]).options.all).toBe(true);
|
||||
});
|
||||
|
||||
test("value flags consume exactly one token", () => {
|
||||
const parsed = parseArgs(["approval-runs", "list", "--limit", "25", "--status", "completed"]);
|
||||
|
||||
expect(parsed.options.limit).toBe("25");
|
||||
expect(parsed.options.status).toBe("completed");
|
||||
expect(parsed.command).toEqual(["approval-runs", "list"]);
|
||||
});
|
||||
|
||||
test("value flag with a missing value is a usage error, not a silent drop", () => {
|
||||
// numberOption(true) previously returned undefined, so a bare `--limit`
|
||||
// silently meant "no limit" and pulled the full dataset.
|
||||
expect(() => parseArgs(["approval-runs", "list", "--limit"])).toThrow(
|
||||
"Option --limit requires a value."
|
||||
);
|
||||
expect(() => parseArgs(["approval-runs", "list", "--limit", "--status", "completed"])).toThrow(
|
||||
"Option --limit requires a value."
|
||||
);
|
||||
});
|
||||
|
||||
test("value flags accept negative numbers as values", () => {
|
||||
expect(parseArgs(["corps", "search", "x", "--offset", "-5"]).options.offset).toBe("-5");
|
||||
});
|
||||
|
||||
test("unknown flags are rejected with a did-you-mean suggestion", () => {
|
||||
// `corps list --limitt 5` used to exit 0 while silently ignoring the flag.
|
||||
expect(() => parseArgs(["corps", "list", "--limitt", "5"])).toThrow(
|
||||
"Unknown option: --limitt. Did you mean --limit?"
|
||||
);
|
||||
expect(() => parseArgs(["corps", "list", "--bogus-flag"])).toThrow(
|
||||
"Unknown option: --bogus-flag."
|
||||
);
|
||||
});
|
||||
|
||||
test("kebab and camel spellings of the same flag both stay registered", () => {
|
||||
const kebab = parseArgs(["corps", "list", "--record-id", "abc"]);
|
||||
const camel = parseArgs(["corps", "list", "--recordId", "abc"]);
|
||||
|
||||
expect(kebab.options["record-id"]).toBe("abc");
|
||||
expect(camel.options.recordId).toBe("abc");
|
||||
});
|
||||
|
||||
test("the = value form bypasses next-token consumption", () => {
|
||||
expect(parseArgs(["rules", "get", "--code=0038"]).options.code).toBe("0038");
|
||||
expect(parseArgs(["corps", "list", "--all=true"]).options.all).toBe("true");
|
||||
});
|
||||
|
||||
test("ambiguous flags (f / h / version) keep the legacy heuristic", () => {
|
||||
expect(parseArgs(["rules", "lint", "--f", "/tmp/rules.json"]).options.f).toBe("/tmp/rules.json");
|
||||
expect(parseArgs(["auth", "hose-link", "--f"]).options.f).toBe(true);
|
||||
expect(parseArgs(["http", "--h", "127.0.0.1"]).options.h).toBe("127.0.0.1");
|
||||
expect(parseArgs(["corps", "list", "--help"]).options.help).toBe(true);
|
||||
// Bare --version (no area) must stay boolean: it is the version alias.
|
||||
expect(parseArgs(["--version"]).options.version).toBe(true);
|
||||
expect(parseArgs(["--v"]).options.v).toBe(true);
|
||||
expect(parseArgs(["--V"]).options.V).toBe(true);
|
||||
// With a value it must consume it: rules/update read --version as a value.
|
||||
expect(parseArgs(["rules", "get", "--version", "latest"]).options.version).toBe("latest");
|
||||
});
|
||||
|
||||
test("--json still parses its payload and rejects non-objects downstream", () => {
|
||||
expect(parseArgs(["corps", "add", "--json", '{"name":"Acme"}']).json).toEqual({ name: "Acme" });
|
||||
expect(() => parseArgs(["corps", "add", "--json"])).toThrow("--json requires a JSON object argument");
|
||||
expect(() => parseArgs(["corps", "add", "--json", "not-json"])).toThrow("Invalid JSON:");
|
||||
});
|
||||
});
|
||||
|
||||
describe("option helpers", () => {
|
||||
test("numberOption rejects non-numeric input", () => {
|
||||
expect(numberOption("25")).toBe(25);
|
||||
expect(numberOption(undefined)).toBeUndefined();
|
||||
expect(() => numberOption("abc")).toThrow("Invalid number: abc");
|
||||
});
|
||||
|
||||
test("booleanOption accepts true/false strings and rejects other values", () => {
|
||||
expect(booleanOption("true")).toBe(true);
|
||||
expect(booleanOption("false")).toBe(false);
|
||||
expect(booleanOption(true)).toBe(true);
|
||||
expect(() => booleanOption("now")).toThrow("Invalid boolean: now");
|
||||
});
|
||||
|
||||
test("stringOption drops bare booleans so a missing value cannot masquerade", () => {
|
||||
expect(stringOption("x")).toBe("x");
|
||||
expect(stringOption(true)).toBeUndefined();
|
||||
});
|
||||
|
||||
test("splitOption trims and drops empty entries", () => {
|
||||
expect(splitOption("a, b ,,c")).toEqual(["a", "b", "c"]);
|
||||
expect(splitOption("")).toBeUndefined();
|
||||
expect(splitOption(true)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("flag registry cross-check", () => {
|
||||
test("every flag documented in help.ts is registered", () => {
|
||||
const help = readFileSync(join(import.meta.dirname, "help.ts"), "utf-8");
|
||||
const documented = new Set(
|
||||
[...help.matchAll(/flag: "--([a-z][a-z0-9-]*)"/g)].map((match) => match[1]!)
|
||||
);
|
||||
for (const flag of documented) {
|
||||
expect(KNOWN_FLAGS.has(flag)).toBe(true);
|
||||
}
|
||||
expect(documented.size).toBeGreaterThanOrEqual(20);
|
||||
});
|
||||
|
||||
test("every flag shown in a help usage line is registered", () => {
|
||||
const help = readFileSync(join(import.meta.dirname, "help.ts"), "utf-8");
|
||||
const usageFlags = new Set(
|
||||
[...help.matchAll(/ --([a-z][a-z0-9-]*)/g)].map((match) => match[1]!)
|
||||
);
|
||||
const missing = [...usageFlags].filter((flag) => !KNOWN_FLAGS.has(flag));
|
||||
expect(missing).toEqual([]);
|
||||
});
|
||||
|
||||
test("boolean and value sets partition the registry apart from the ambiguous three", () => {
|
||||
// Guards the invariant the parser relies on: no flag may be in both sets.
|
||||
const both = [...KNOWN_FLAGS].filter(
|
||||
(flag) => BOOLEAN_FLAGS.has(flag) && VALUE_FLAGS.has(flag)
|
||||
);
|
||||
expect(both).toEqual([]);
|
||||
|
||||
// The union plus the ambiguous three must cover the whole registry, so a
|
||||
// flag can never fall through to an unintended branch.
|
||||
const union = new Set([...BOOLEAN_FLAGS, ...VALUE_FLAGS, "f", "h", "version"]);
|
||||
expect([...KNOWN_FLAGS].every((flag) => union.has(flag))).toBe(true);
|
||||
expect(KNOWN_FLAGS.size).toBe(union.size);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user