@@ -7,7 +7,7 @@ import { createWebFetchTool } from "../src/agents/tools/web-fetch.js";
77import type { OpenClawConfig } from "../src/config/types.openclaw.js" ;
88import type { LookupFn } from "../src/infra/net/ssrf.js" ;
99import { extractReadableContent } from "../src/web-fetch/content-extractors.runtime.js" ;
10- import { stripLeadingPackageManagerSeparator } from "./lib/arg-utils.mts" ;
10+ import * as cliArgs from "./lib/arg-utils.mts" ;
1111
1212type BenchmarkCaseId =
1313 | "tool-create"
@@ -68,8 +68,7 @@ const ALL_CASE_IDS = [
6868 "extract-basic-shell" ,
6969] as const satisfies readonly BenchmarkCaseId [ ] ;
7070
71- const BOOLEAN_FLAGS = new Set ( [ "--help" , "-h" , "--json" ] ) ;
72- const VALUE_FLAGS = new Set ( [ "--case" , "--output" , "--runs" , "--warmup" ] ) ;
71+ const SPLIT_VALUE_FLAG_OPTIONS = { allowInline : false , rejectShortOptions : true } as const ;
7372
7473class CliArgumentError extends Error {
7574 override name = "CliArgumentError" ;
@@ -125,71 +124,7 @@ const toolConfig: OpenClawConfig = {
125124 } ,
126125} ;
127126
128- function validateArgs ( args : string [ ] ) : void {
129- const seenSingularValueFlags = new Set < string > ( ) ;
130- for ( let index = 0 ; index < args . length ; index += 1 ) {
131- const arg = args [ index ] ?? "" ;
132- if ( BOOLEAN_FLAGS . has ( arg ) ) {
133- continue ;
134- }
135- if ( VALUE_FLAGS . has ( arg ) ) {
136- if ( arg !== "--case" ) {
137- if ( seenSingularValueFlags . has ( arg ) ) {
138- throw new CliArgumentError ( `${ arg } was provided more than once` ) ;
139- }
140- seenSingularValueFlags . add ( arg ) ;
141- }
142- const value = args [ index + 1 ] ;
143- if ( ! value || value . startsWith ( "-" ) ) {
144- throw new CliArgumentError ( `${ arg } requires a value` ) ;
145- }
146- index += 1 ;
147- continue ;
148- }
149- throw new CliArgumentError ( `Unknown argument: ${ arg } ` ) ;
150- }
151- }
152-
153- function parsePositiveInteger ( flag : string , fallback : number , args : string [ ] ) : number {
154- const index = args . indexOf ( flag ) ;
155- if ( index === - 1 ) {
156- return fallback ;
157- }
158- const raw = args [ index + 1 ] ;
159- if ( ! / ^ \d + $ / u. test ( raw ?? "" ) ) {
160- throw new CliArgumentError ( `${ flag } must be a positive integer` ) ;
161- }
162- const value = Number ( raw ) ;
163- if ( ! Number . isSafeInteger ( value ) || value <= 0 ) {
164- throw new CliArgumentError ( `${ flag } must be a positive integer` ) ;
165- }
166- return value ;
167- }
168-
169- function parseNonNegativeInteger ( flag : string , fallback : number , args : string [ ] ) : number {
170- const index = args . indexOf ( flag ) ;
171- if ( index === - 1 ) {
172- return fallback ;
173- }
174- const raw = args [ index + 1 ] ;
175- if ( ! / ^ \d + $ / u. test ( raw ?? "" ) ) {
176- throw new CliArgumentError ( `${ flag } must be a non-negative integer` ) ;
177- }
178- const value = Number ( raw ) ;
179- if ( ! Number . isSafeInteger ( value ) || value < 0 ) {
180- throw new CliArgumentError ( `${ flag } must be a non-negative integer` ) ;
181- }
182- return value ;
183- }
184-
185- function parseCases ( args : string [ ] ) : BenchmarkCaseId [ ] {
186- const requested : string [ ] = [ ] ;
187- for ( let index = 0 ; index < args . length ; index += 1 ) {
188- if ( args [ index ] === "--case" ) {
189- requested . push ( args [ index + 1 ] ?? "" ) ;
190- index += 1 ;
191- }
192- }
127+ function parseCases ( requested : string [ ] ) : BenchmarkCaseId [ ] {
193128 if ( requested . length === 0 || requested . includes ( "all" ) ) {
194129 return [ ...ALL_CASE_IDS ] ;
195130 }
@@ -204,21 +139,51 @@ function parseCases(args: string[]): BenchmarkCaseId[] {
204139 return requested as BenchmarkCaseId [ ] ;
205140}
206141
207- function parseFlagValue ( flag : string , args : string [ ] ) : string | undefined {
208- const index = args . indexOf ( flag ) ;
209- return index === - 1 ? undefined : args [ index + 1 ] ;
142+ function parseBenchmarkCount ( raw : string , flag : string , minimum : 0 | 1 ) : number {
143+ const parsed = cliArgs . classifyBoundedUnsignedDecimal ( raw , minimum , Number . MAX_SAFE_INTEGER ) ;
144+ if ( parsed . kind === "value" ) {
145+ return parsed . value ;
146+ }
147+ const kind = minimum === 0 ? "non-negative" : "positive" ;
148+ throw new CliArgumentError ( `${ flag } must be a ${ kind } integer` ) ;
210149}
211150
212151function parseOptions ( args = process . argv . slice ( 2 ) ) : Options {
213- const normalizedArgs = stripLeadingPackageManagerSeparator ( args ) ;
214- validateArgs ( normalizedArgs ) ;
215- return {
216- cases : parseCases ( normalizedArgs ) ,
217- json : normalizedArgs . includes ( "--json" ) ,
218- output : parseFlagValue ( "--output" , normalizedArgs ) ,
219- runs : parsePositiveInteger ( "--runs" , 20 , normalizedArgs ) ,
220- warmup : parseNonNegativeInteger ( "--warmup" , 3 , normalizedArgs ) ,
221- } ;
152+ const normalizedArgs = cliArgs . stripLeadingPackageManagerSeparator ( args ) ;
153+ try {
154+ const parsed = cliArgs . parseFlagArgs (
155+ normalizedArgs ,
156+ {
157+ cases : [ ] as string [ ] ,
158+ json : false ,
159+ output : undefined as string | undefined ,
160+ runs : "20" ,
161+ warmup : "3" ,
162+ } ,
163+ [
164+ cliArgs . stringListFlag ( "--case" , "cases" , SPLIT_VALUE_FLAG_OPTIONS ) ,
165+ cliArgs . stringFlag ( "--output" , "output" , SPLIT_VALUE_FLAG_OPTIONS ) ,
166+ cliArgs . stringFlag ( "--runs" , "runs" , SPLIT_VALUE_FLAG_OPTIONS ) ,
167+ cliArgs . stringFlag ( "--warmup" , "warmup" , SPLIT_VALUE_FLAG_OPTIONS ) ,
168+ cliArgs . booleanFlag ( "--json" , "json" , true , { repeatable : true } ) ,
169+ ] ,
170+ {
171+ ignoreDoubleDash : false ,
172+ onUnhandledArg ( arg ) {
173+ throw new CliArgumentError ( `Unknown argument: ${ arg } ` ) ;
174+ } ,
175+ } ,
176+ ) ;
177+ return {
178+ cases : parseCases ( parsed . cases ) ,
179+ json : parsed . json ,
180+ output : parsed . output ,
181+ runs : parseBenchmarkCount ( parsed . runs , "--runs" , 1 ) ,
182+ warmup : parseBenchmarkCount ( parsed . warmup , "--warmup" , 0 ) ,
183+ } ;
184+ } catch ( error ) {
185+ throw new CliArgumentError ( error instanceof Error ? error . message : String ( error ) ) ;
186+ }
222187}
223188
224189function printUsage ( ) : void {
0 commit comments