



















@@ -26,6 +26,23 @@ type Options = {
2626warmup: number;
2727};
282829+const BOOLEAN_FLAGS = new Set(["--help", "-h", "--json", "--keep-temp", "--runtime-hooks"]);
30+const VALUE_FLAGS = new Set([
31+"--agents",
32+"--cpu-prof-dir",
33+"--cpu-prof-output",
34+"--lookups",
35+"--models-per-provider",
36+"--output",
37+"--providers",
38+"--runs",
39+"--warmup",
40+]);
41+42+class CliArgumentError extends Error {
43+override name = "CliArgumentError";
44+}
45+2946type PhaseSample = {
3047ensureMs: number;
3148resolveMs: number;
@@ -73,7 +90,11 @@ function parseFlagValue(flag: string): string | undefined {
7390if (index === -1) {
7491return undefined;
7592}
76-return process.argv[index + 1];
93+const value = process.argv[index + 1];
94+if (!value || value.startsWith("--")) {
95+throw new CliArgumentError(`${flag} requires a value`);
96+}
97+return value;
7798}
789979100function hasFlag(flag: string): boolean {
@@ -85,9 +106,12 @@ function parsePositiveInt(flag: string, fallback: number): number {
85106if (!raw) {
86107return fallback;
87108}
88-const value = Number.parseInt(raw, 10);
109+const value = Number(raw);
89110if (!Number.isFinite(value) || value <= 0) {
90-throw new Error(`${flag} must be a positive integer`);
111+throw new CliArgumentError(`${flag} must be a positive integer`);
112+}
113+if (!Number.isInteger(value)) {
114+throw new CliArgumentError(`${flag} must be a positive integer`);
91115}
92116return value;
93117}
@@ -97,14 +121,32 @@ function parseNonNegativeInt(flag: string, fallback: number): number {
97121if (!raw) {
98122return fallback;
99123}
100-const value = Number.parseInt(raw, 10);
124+const value = Number(raw);
101125if (!Number.isFinite(value) || value < 0) {
102-throw new Error(`${flag} must be a non-negative integer`);
126+throw new CliArgumentError(`${flag} must be a non-negative integer`);
127+}
128+if (!Number.isInteger(value)) {
129+throw new CliArgumentError(`${flag} must be a non-negative integer`);
103130}
104131return value;
105132}
106133134+function validateCliArgs(args = process.argv.slice(2)): void {
135+for (let index = 0; index < args.length; index += 1) {
136+const arg = args[index] ?? "";
137+if (BOOLEAN_FLAGS.has(arg)) {
138+continue;
139+}
140+if (VALUE_FLAGS.has(arg)) {
141+index += 1;
142+continue;
143+}
144+throw new CliArgumentError(`Unknown argument: ${arg}`);
145+}
146+}
147+107148function parseOptions(): Options {
149+validateCliArgs();
108150return {
109151agentCount: parsePositiveInt("--agents", 8),
110152cpuProfDir: parseFlagValue("--cpu-prof-dir"),
@@ -494,6 +536,10 @@ async function main(): Promise<void> {
494536}
495537496538main().catch((error: unknown) => {
539+if (error instanceof CliArgumentError) {
540+process.stderr.write(`${error.message}\n`);
541+process.exit(1);
542+}
497543const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
498544process.stderr.write(`${message}\n`);
499545process.exit(1);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。