CLI: dedupe config validate errors and expose allowed values

This commit is contained in:
Gustavo Madeira Santana
2026-03-02 20:05:12 -05:00
parent a44843507f
commit f26853f14c
41 changed files with 1393 additions and 134 deletions

View File

@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { consumeRootOptionToken } from "./cli-root-options.js";
describe("consumeRootOptionToken", () => {
it("consumes boolean and inline root options", () => {
expect(consumeRootOptionToken(["--dev"], 0)).toBe(1);
expect(consumeRootOptionToken(["--profile=work"], 0)).toBe(1);
expect(consumeRootOptionToken(["--log-level=debug"], 0)).toBe(1);
});
it("consumes split root value option only when next token is a value", () => {
expect(consumeRootOptionToken(["--profile", "work"], 0)).toBe(2);
expect(consumeRootOptionToken(["--profile", "--no-color"], 0)).toBe(1);
expect(consumeRootOptionToken(["--profile", "--"], 0)).toBe(1);
});
});

View File

@@ -0,0 +1,31 @@
export const FLAG_TERMINATOR = "--";
const ROOT_BOOLEAN_FLAGS = new Set(["--dev", "--no-color"]);
const ROOT_VALUE_FLAGS = new Set(["--profile", "--log-level"]);
export function isValueToken(arg: string | undefined): boolean {
if (!arg || arg === FLAG_TERMINATOR) {
return false;
}
if (!arg.startsWith("-")) {
return true;
}
return /^-\d+(?:\.\d+)?$/.test(arg);
}
export function consumeRootOptionToken(args: ReadonlyArray<string>, index: number): number {
const arg = args[index];
if (!arg) {
return 0;
}
if (ROOT_BOOLEAN_FLAGS.has(arg)) {
return 1;
}
if (arg.startsWith("--profile=") || arg.startsWith("--log-level=")) {
return 1;
}
if (ROOT_VALUE_FLAGS.has(arg)) {
return isValueToken(args[index + 1]) ? 2 : 1;
}
return 0;
}