
























@@ -110,20 +110,45 @@ type CliOptions = {
110110stateDir: string | null;
111111};
112112113-function parseFlagValue(flag: string): string | undefined {
114-const index = process.argv.indexOf(flag);
113+const BOOLEAN_FLAGS = new Set(["--help"]);
114+const VALUE_FLAGS = new Set(["--output", "--profile", "--state-dir"]);
115+116+class CliUsageError extends Error {
117+override name = "CliUsageError";
118+}
119+120+function parseFlagValue(flag: string, argv: string[]): string | undefined {
121+const index = argv.indexOf(flag);
115122if (index === -1) {
116123return undefined;
117124}
118-const value = process.argv[index + 1];
125+const value = argv[index + 1];
119126if (!value || value.startsWith("--")) {
120-throw new Error(`${flag} requires a value`);
127+throw new CliUsageError(`${flag} requires a value`);
121128}
122129return value;
123130}
124131125-function hasFlag(flag: string): boolean {
126-return process.argv.includes(flag);
132+function hasFlag(flag: string, argv = process.argv.slice(2)): boolean {
133+return argv.includes(flag);
134+}
135+136+function validateArgs(argv: string[]): void {
137+for (let index = 0; index < argv.length; index += 1) {
138+const arg = argv[index] ?? "";
139+if (BOOLEAN_FLAGS.has(arg)) {
140+continue;
141+}
142+if (VALUE_FLAGS.has(arg)) {
143+const value = argv[index + 1];
144+if (!value || value.startsWith("--")) {
145+throw new CliUsageError(`${arg} requires a value`);
146+}
147+index += 1;
148+continue;
149+}
150+throw new CliUsageError(`Unknown argument: ${arg}`);
151+}
127152}
128153129154function parseProfile(raw: string | undefined): ProfileId {
@@ -133,14 +158,17 @@ function parseProfile(raw: string | undefined): ProfileId {
133158if (raw === "smoke" || raw === "default" || raw === "large") {
134159return raw;
135160}
136-throw new Error(`--profile must be one of smoke, default, large; got ${JSON.stringify(raw)}`);
161+throw new CliUsageError(
162+`--profile must be one of smoke, default, large; got ${JSON.stringify(raw)}`,
163+);
137164}
138165139-function parseOptions(): CliOptions {
166+function parseOptions(argv = process.argv.slice(2)): CliOptions {
167+validateArgs(argv);
140168return {
141-output: parseFlagValue("--output") ?? null,
142-profile: parseProfile(parseFlagValue("--profile")),
143-stateDir: parseFlagValue("--state-dir") ?? null,
169+output: parseFlagValue("--output", argv) ?? null,
170+profile: parseProfile(parseFlagValue("--profile", argv)),
171+stateDir: parseFlagValue("--state-dir", argv) ?? null,
144172};
145173}
146174@@ -535,11 +563,13 @@ function printProofLines(report: BenchmarkReport): void {
535563}
536564537565function main(): void {
538-if (hasFlag("--help")) {
566+const argv = process.argv.slice(2);
567+validateArgs(argv);
568+if (hasFlag("--help", argv)) {
539569printUsage();
540570return;
541571}
542-const options = parseOptions();
572+const options = parseOptions(argv);
543573const config = applyScale(PROFILES[options.profile]);
544574const stateDir =
545575options.stateDir ?? fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-perf-"));
@@ -626,5 +656,13 @@ function main(): void {
626656}
627657628658if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
629-main();
659+try {
660+main();
661+} catch (error) {
662+if (error instanceof CliUsageError) {
663+console.error(`error: ${error.message}`);
664+process.exit(2);
665+}
666+throw error;
667+}
630668}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。