@@ -66,42 +66,7 @@ async function main(): Promise<void> {
6666 printHelp ( 1 ) ;
6767 }
6868
69- let host = '127.0.0.1' ;
70- let port = 8137 ;
71- let outDir = 'artifacts/e2e' ;
72- let expectedRuns = 1 ;
73- let timeoutMs = parseDurationToMs ( '20m' ) ;
74- let watch = false ;
75-
76- for ( let i = 0 ; i < args . length ; i ++ ) {
77- const arg = args [ i ] ;
78- const next = args [ i + 1 ] ;
79-
80- if ( arg === '--host' && next ) {
81- host = next ;
82- i ++ ;
83- } else if ( arg === '--port' && next ) {
84- port = Number ( next ) ;
85- i ++ ;
86- } else if ( arg === '--out' && next ) {
87- outDir = next ;
88- i ++ ;
89- } else if ( arg === '--expected-runs' && next ) {
90- expectedRuns = Number ( next ) ;
91- i ++ ;
92- } else if ( arg === '--timeout' && next ) {
93- timeoutMs = parseDurationToMs ( next ) ;
94- i ++ ;
95- } else if ( arg === '--watch' ) {
96- watch = true ;
97- } else if ( arg === '--help' || arg === '-h' ) {
98- printHelp ( 0 ) ;
99- } else {
100- // eslint-disable-next-line no-console
101- console . error ( `Unknown argument: ${ arg } ` ) ;
102- printHelp ( 1 ) ;
103- }
104- }
69+ let { host, port, outDir, expectedRuns, timeoutMs, watch } = parseArgs ( args ) ;
10570
10671 if ( ! Number . isFinite ( port ) || port <= 0 ) {
10772 throw new Error ( `Invalid --port value '${ port } '.` ) ;
@@ -137,3 +102,75 @@ main().catch((e) => {
137102 console . error ( e ?. stack ?? String ( e ) ) ;
138103 process . exit ( 2 ) ;
139104} ) ;
105+
106+ type CliArgs = {
107+ host : string ;
108+ port: number ;
109+ outDir: string ;
110+ expectedRuns: number ;
111+ timeoutMs: number ;
112+ watch: boolean ;
113+ } ;
114+
115+ function parseArgs ( args : string [ ] ) : CliArgs {
116+ const options : CliArgs = {
117+ host : '127.0.0.1' ,
118+ port : 8137 ,
119+ outDir : 'artifacts/e2e' ,
120+ expectedRuns : 1 ,
121+ timeoutMs : parseDurationToMs ( '20m' ) ,
122+ watch : false ,
123+ } ;
124+
125+ for ( let i = 0 ; i < args . length ; ) {
126+ const arg = args [ i ] ;
127+
128+ switch ( arg ) {
129+ case '--host' :
130+ options . host = requireValue ( arg , args [ i + 1 ] ) ;
131+ i += 2 ;
132+ break ;
133+ case '--port' :
134+ options . port = Number ( requireValue ( arg , args [ i + 1 ] ) ) ;
135+ i += 2 ;
136+ break ;
137+ case '--out' :
138+ options . outDir = requireValue ( arg , args [ i + 1 ] ) ;
139+ i += 2 ;
140+ break ;
141+ case '--expected-runs' :
142+ options . expectedRuns = Number ( requireValue ( arg , args [ i + 1 ] ) ) ;
143+ i += 2 ;
144+ break ;
145+ case '--timeout' :
146+ options . timeoutMs = parseDurationToMs ( requireValue ( arg , args [ i + 1 ] ) ) ;
147+ i += 2 ;
148+ break ;
149+ case '--watch' :
150+ options . watch = true ;
151+ i += 1 ;
152+ break ;
153+ case '--help' :
154+ case '-h' :
155+ printHelp ( 0 ) ;
156+ return options ;
157+ default :
158+ // eslint-disable-next-line no-console
159+ console . error ( `Unknown argument: ${ arg } ` ) ;
160+ printHelp ( 1 ) ;
161+ return options ;
162+ }
163+ }
164+
165+ return options ;
166+ }
167+
168+ function requireValue ( flag : string , value : string | undefined ) : string {
169+ if ( ! value ) {
170+ // eslint-disable-next-line no-console
171+ console. error ( `Missing value for ${ flag } .` ) ;
172+ printHelp ( 1 ) ;
173+ return '' ;
174+ }
175+ return value ;
176+ }
0 commit comments