mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-12 22:22:54 +00:00
CLI: dedupe config validate errors and expose allowed values
This commit is contained in:
16
src/infra/cli-root-options.test.ts
Normal file
16
src/infra/cli-root-options.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
31
src/infra/cli-root-options.ts
Normal file
31
src/infra/cli-root-options.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user