mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
c4fa5b475c
* fix(cli)!: accept only the documented kebab-case option spelling REA's argv parser kept incur's second spelling of every option: the camelCase schema key (`--maxRows`) bound like the documented `--max-rows`, although help, completion and manifests list only the kebab-case form. That hidden alias let wrong spellings survive in the guides. The parser now binds only the documented flag. A schema key spelling, including its `--no-` negation, fails as a typed `flag_spelling` failure rendered as PARSE_ERROR that names the documented flag: "Unknown flag: --maxRows. Did you mean --max-rows?". Completion no longer resolves camelCase option names. The sensitive-value scan still treats `--sensitiveValue` as a declaration so its value stays redacted from the refusal. Guides, the inspect-api-contract option description and tests now use the kebab-case flags. BREAKING CHANGE: camelCase option spellings such as `--maxRows` or `--dryRun` are refused; use the kebab-case flag that `--help` lists. * test(cli): exercise invalid dry-run values with the documented flag spelling The invalid dry-run cases in strictPositionals and cliSetup appended --dryRun=false as a later valid override. The parser now refuses that schema key spelling before it validates the dry-run value, so each case failed on flag spelling and never checked that a later override cannot hide an earlier invalid value. The override is now --dry-run=false, and each case asserts the dry-run failure message, so a spelling refusal can no longer satisfy it. The --dryRun=abc case in cliSetup's malformed-value list is removed: it was the --dry-run=abc case under a spelling the parser now refuses before reading the value, and argvParser.test.ts owns that refusal. * test(cli): keep one public case for the flag spelling refusal cliOptionAliases repeated argvParser's three token shapes (bare, inline value, negation) through the full CLI. The parser test owns those shapes and asserts the structured flag_spelling failure with its flag and documented spelling; the CLI keeps one case for the documented PARSE_ERROR projection, whose message is part of the CLI contract. The camel-case sensitive-value case also dropped its trailing --no-<literal> token: the spelling refusal stops parsing before it, so the token no longer exercised anything.
303 lines
11 KiB
TypeScript
303 lines
11 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import {
|
|
declaredOptionType,
|
|
isCountOption,
|
|
optionFlagName,
|
|
} from "./optionFields.js";
|
|
|
|
/**
|
|
* REA's argv parser for one command: binds tokens to the command's
|
|
* positionals and options, then validates them with the command's schemas.
|
|
* Option keys are camelCase; each is spelled only `--kebab-case` on the
|
|
* command line. The schema key spelling (`--camelCase`) is refused with the
|
|
* documented flag as its suggestion. A boolean option is set by its
|
|
* bare flag and cleared by `--no-<flag>`; `--flag=value` and `--flag value`
|
|
* both bind a value; an array option collects every occurrence; a short
|
|
* alias may be stacked (`-ab`) when all but the last are booleans or
|
|
* counts; a count option adds one per occurrence (`-vvv`). `--` is
|
|
* not a separator here: after the command it is an unknown flag. The
|
|
* messages and field errors match what REA has always printed (#11654).
|
|
*/
|
|
|
|
/** One field that failed validation, as REA prints it. */
|
|
export interface CliFieldError {
|
|
readonly code: string;
|
|
/** The caller did not supply the field at all. */
|
|
readonly missing: boolean;
|
|
/** Dotted path within the positionals or options. */
|
|
readonly path: string;
|
|
readonly expected: string;
|
|
readonly received: string;
|
|
readonly message: string;
|
|
}
|
|
|
|
/**
|
|
* Why argv was refused: a token the parser cannot bind (an unknown flag or a
|
|
* missing value), or bound positionals or options their schema refused.
|
|
*/
|
|
export type CliArgvFailure =
|
|
| { readonly kind: "parse"; readonly message: string }
|
|
| {
|
|
/** An option's schema key spelling, which argv does not accept. */
|
|
readonly kind: "flag_spelling";
|
|
readonly message: string;
|
|
/** The refused token's flag, such as `--maxRows` or `--no-dryRun`. */
|
|
readonly flag: string;
|
|
/** The documented spelling of the same flag, such as `--max-rows`. */
|
|
readonly documented: string;
|
|
}
|
|
| { readonly kind: "surplus"; readonly message: string }
|
|
| { readonly kind: "dry_run"; readonly message: string }
|
|
| {
|
|
readonly kind: "validation";
|
|
readonly message: string;
|
|
readonly fieldErrors: readonly CliFieldError[];
|
|
/** Original parser facts for command-specific safe constraint projection. */
|
|
readonly issues: readonly z.core.$ZodIssue[];
|
|
};
|
|
|
|
const parseFailure = (message: string): Result<never, CliArgvFailure> =>
|
|
err({ kind: "parse", message });
|
|
|
|
export interface CliArgvSchemas {
|
|
/** Positionals in argv order; a final array field takes the rest. */
|
|
readonly args?: z.ZodObject | undefined;
|
|
readonly options?: z.ZodObject | undefined;
|
|
/** Short alias by option key, such as `{ yes: "y" }`. */
|
|
readonly alias?: Readonly<Record<string, string>> | undefined;
|
|
}
|
|
|
|
export interface CliParsedArgv {
|
|
readonly args: Record<string, unknown>;
|
|
readonly options: Record<string, unknown>;
|
|
}
|
|
|
|
const fieldOf = (
|
|
schema: z.ZodObject | undefined,
|
|
name: string,
|
|
): z.ZodType | undefined =>
|
|
schema === undefined
|
|
? undefined
|
|
: (schema.shape as Readonly<Record<string, z.ZodType>>)[name];
|
|
|
|
const isBoolean = (schema: z.ZodObject | undefined, name: string): boolean => {
|
|
const field = fieldOf(schema, name);
|
|
return (
|
|
field !== undefined && declaredOptionType(field) instanceof z.ZodBoolean
|
|
);
|
|
};
|
|
|
|
/** A count option (`.meta({ count: true })`) adds one per occurrence. */
|
|
const isCount = (schema: z.ZodObject | undefined, name: string): boolean => {
|
|
const field = fieldOf(schema, name);
|
|
return field !== undefined && isCountOption(field);
|
|
};
|
|
|
|
const isArray = (schema: z.ZodObject | undefined, name: string): boolean => {
|
|
const field = fieldOf(schema, name);
|
|
return field !== undefined && declaredOptionType(field) instanceof z.ZodArray;
|
|
};
|
|
|
|
/** Number and boolean options arrive as text; the schema validates the result. */
|
|
const coerce = (value: unknown, schema: z.ZodObject, name: string): unknown => {
|
|
const field = fieldOf(schema, name);
|
|
if (field === undefined || typeof value !== "string") return value;
|
|
const type = declaredOptionType(field);
|
|
if (type instanceof z.ZodNumber) return Number(value);
|
|
if (type instanceof z.ZodBoolean) return value === "true";
|
|
return value;
|
|
};
|
|
|
|
/** Whether the raw input holds the issue's whole path. */
|
|
const hasPath = (data: unknown, path: readonly PropertyKey[]): boolean => {
|
|
let current = data;
|
|
for (const part of path) {
|
|
if (typeof current !== "object" || current === null) return false;
|
|
if (!(part in current)) return false;
|
|
current = (current as Record<PropertyKey, unknown>)[part];
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/** Validate with `schema`, reporting each issue as a field error. */
|
|
export const validateCliInputs = (
|
|
schema: z.ZodObject,
|
|
data: Readonly<Record<string, unknown>>,
|
|
): Result<Record<string, unknown>, CliArgvFailure> => {
|
|
const parsed = schema.safeParse(data);
|
|
if (parsed.success) return ok(parsed.data);
|
|
const { issues } = parsed.error;
|
|
return err({
|
|
kind: "validation",
|
|
issues,
|
|
message:
|
|
issues.map(({ message }) => message).join("; ") || "Validation failed",
|
|
fieldErrors: issues.map((issue) => ({
|
|
code: issue.code,
|
|
missing: !hasPath(data, issue.path),
|
|
path: issue.path.join("."),
|
|
expected: stringField(issue, "expected"),
|
|
received: stringField(issue, "received"),
|
|
message: issue.message,
|
|
})),
|
|
});
|
|
};
|
|
|
|
/** Zod reports `expected` (and older issues `received`) as text when present. */
|
|
const stringField = (issue: object, key: string): string =>
|
|
(Reflect.get(issue, key) as string | undefined) ?? "";
|
|
|
|
/** Refuse an unknown flag, naming the documented one for a schema key. */
|
|
const unknownFlagFailure = (
|
|
known: ReadonlySet<string>,
|
|
prefix: "--" | "--no-",
|
|
raw: string,
|
|
): Result<never, CliArgvFailure> => {
|
|
const flag = `${prefix}${raw}`;
|
|
if (!known.has(raw)) return parseFailure(`Unknown flag: ${flag}`);
|
|
const documented = `${prefix}${optionFlagName(raw)}`;
|
|
return err({
|
|
kind: "flag_spelling",
|
|
message: `Unknown flag: ${flag}. Did you mean ${documented}?`,
|
|
flag,
|
|
documented,
|
|
});
|
|
};
|
|
|
|
/** Bind and validate one command's argv. */
|
|
export const parseCliArgv = (
|
|
argv: readonly string[],
|
|
schemas: CliArgvSchemas,
|
|
): Result<CliParsedArgv, CliArgvFailure> => {
|
|
const { args: argsSchema, options: optionsSchema, alias } = schemas;
|
|
const known = new Set(
|
|
optionsSchema === undefined ? [] : Object.keys(optionsSchema.shape),
|
|
);
|
|
/** The documented `--flag` name for each option key. */
|
|
const keyOf = new Map([...known].map((name) => [optionFlagName(name), name]));
|
|
const optionName = (raw: string): string | undefined => keyOf.get(raw);
|
|
const unknownFlag = (prefix: "--" | "--no-", raw: string) =>
|
|
unknownFlagFailure(known, prefix, raw);
|
|
const aliasName = new Map(
|
|
Object.entries(alias ?? {}).map(([name, short]) => [short, name] as const),
|
|
);
|
|
const positionals: string[] = [];
|
|
const options: Record<string, unknown> = {};
|
|
let invalidDryRun = false;
|
|
const set = (name: string, value: string): void => {
|
|
if (name === "dryRun" && isBoolean(optionsSchema, name))
|
|
invalidDryRun ||= value !== "true" && value !== "false";
|
|
if (!isArray(optionsSchema, name)) {
|
|
options[name] = value;
|
|
return;
|
|
}
|
|
const existing = options[name];
|
|
if (Array.isArray(existing)) existing.push(value);
|
|
else options[name] = [value];
|
|
};
|
|
for (let index = 0; index < argv.length; index += 1) {
|
|
const token = argv[index]!;
|
|
if (token.startsWith("--no-") && token.length > 5) {
|
|
const raw = token.slice(5);
|
|
const name = optionName(raw);
|
|
if (name === undefined) return unknownFlag("--no-", raw);
|
|
options[name] = false;
|
|
} else if (token.startsWith("--")) {
|
|
const equals = token.indexOf("=");
|
|
if (equals !== -1) {
|
|
const raw = token.slice(2, equals);
|
|
const name = optionName(raw);
|
|
if (name === undefined) return unknownFlag("--", raw);
|
|
set(name, token.slice(equals + 1));
|
|
continue;
|
|
}
|
|
const name = optionName(token.slice(2));
|
|
if (name === undefined) return unknownFlag("--", token.slice(2));
|
|
if (isCount(optionsSchema, name)) {
|
|
options[name] = ((options[name] as number | undefined) ?? 0) + 1;
|
|
continue;
|
|
}
|
|
if (isBoolean(optionsSchema, name)) {
|
|
options[name] = true;
|
|
continue;
|
|
}
|
|
const value = argv[index + 1];
|
|
if (value === undefined)
|
|
return parseFailure(`Missing value for flag: ${token}`);
|
|
set(name, value);
|
|
index += 1;
|
|
} else if (token.startsWith("-") && token.length >= 2) {
|
|
const shorts = token.slice(1);
|
|
for (const [position, short] of [...shorts].entries()) {
|
|
const name = aliasName.get(short);
|
|
if (name === undefined) return parseFailure(`Unknown flag: -${short}`);
|
|
if (isCount(optionsSchema, name)) {
|
|
options[name] = ((options[name] as number | undefined) ?? 0) + 1;
|
|
continue;
|
|
}
|
|
if (isBoolean(optionsSchema, name)) {
|
|
options[name] = true;
|
|
continue;
|
|
}
|
|
if (position !== shorts.length - 1)
|
|
return parseFailure(
|
|
`Non-boolean flag -${short} must be last in a stacked alias`,
|
|
);
|
|
const value = argv[index + 1];
|
|
if (value === undefined)
|
|
return parseFailure(`Missing value for flag: -${short}`);
|
|
set(name, value);
|
|
index += 1;
|
|
}
|
|
} else {
|
|
positionals.push(token);
|
|
}
|
|
}
|
|
// Check retained token facts before defaults or transforms can run. A later
|
|
// valid occurrence must not hide an earlier invalid dry-run literal.
|
|
if (invalidDryRun)
|
|
return err({
|
|
kind: "dry_run",
|
|
message: "Invalid --dry-run value; use true or false.",
|
|
});
|
|
const keys = Object.keys(argsSchema?.shape ?? {});
|
|
const last = keys.at(-1);
|
|
const variadic = last !== undefined && isArray(argsSchema, last);
|
|
if (!variadic && positionals.length > keys.length)
|
|
return err({
|
|
kind: "surplus",
|
|
message: `Unexpected positional arguments: ${positionals
|
|
.slice(keys.length)
|
|
.map((value) => JSON.stringify(value))
|
|
.join(" ")}.`,
|
|
});
|
|
const rawArgs: Record<string, unknown> = {};
|
|
if (argsSchema !== undefined) {
|
|
for (const [position, key] of keys.entries()) {
|
|
if (isArray(argsSchema, key)) {
|
|
if (position !== keys.length - 1)
|
|
throw new Error(
|
|
`Variadic arg "${key}" must be the last key in the args schema`,
|
|
);
|
|
const rest = positionals.slice(position);
|
|
if (rest.length > 0) rawArgs[key] = rest;
|
|
} else if (positionals[position] !== undefined)
|
|
rawArgs[key] = positionals[position];
|
|
}
|
|
}
|
|
// Positionals are validated first; their failure is reported alone.
|
|
const parsedArgs =
|
|
argsSchema === undefined ? ok({}) : validateCliInputs(argsSchema, rawArgs);
|
|
if (!parsedArgs.ok) return parsedArgs;
|
|
if (optionsSchema === undefined)
|
|
return ok({ args: parsedArgs.value, options: {} });
|
|
for (const [name, value] of Object.entries(options))
|
|
options[name] = coerce(value, optionsSchema, name);
|
|
const parsedOptions = validateCliInputs(optionsSchema, options);
|
|
return parsedOptions.ok
|
|
? ok({ args: parsedArgs.value, options: parsedOptions.value })
|
|
: parsedOptions;
|
|
};
|