@@ -82,6 +82,21 @@ function parseArgs(argv: string[]): {
8282 return { command, flags } ;
8383}
8484
85+ function parseNumericFlag (
86+ flags : Record < string , string | boolean > ,
87+ name : string ,
88+ defaultValue ?: number ,
89+ ) : number | undefined {
90+ const raw = flags [ name ] ;
91+ if ( raw === undefined || raw === true ) return defaultValue ;
92+ const n = parseInt ( raw as string , 10 ) ;
93+ if ( isNaN ( n ) ) {
94+ console . error ( `Invalid value for --${ name } : expected a number` ) ;
95+ process . exit ( 1 ) ;
96+ }
97+ return n ;
98+ }
99+
85100async function main ( ) : Promise < void > {
86101 const { command, flags } = parseArgs ( process . argv . slice ( 2 ) ) ;
87102
@@ -108,7 +123,7 @@ async function main(): Promise<void> {
108123
109124 // ── Daemon management ──
110125 if ( cmd0 === 'start' ) {
111- const port = flags [ 'port' ] ? parseInt ( flags [ 'port' ] as string , 10 ) : undefined ;
126+ const port = parseNumericFlag ( flags , 'port' ) ;
112127 await ensureDaemon ( port ) ;
113128 const resp = await sendCommand ( { type : 'status' } ) ;
114129 if ( resp . ok ) {
@@ -149,9 +164,7 @@ async function main(): Promise<void> {
149164
150165 // ── Component inspection ──
151166 if ( cmd0 === 'get' && cmd1 === 'tree' ) {
152- const depth = flags [ 'depth' ]
153- ? parseInt ( flags [ 'depth' ] as string , 10 )
154- : undefined ;
167+ const depth = parseNumericFlag ( flags , 'depth' ) ;
155168 const ipcCmd : IpcCommand = { type : 'get-tree' , depth } ;
156169 const resp = await sendCommand ( ipcCmd ) ;
157170 if ( resp . ok ) {
@@ -214,7 +227,7 @@ async function main(): Promise<void> {
214227
215228 // ── Wait ──
216229 if ( cmd0 === 'wait' ) {
217- const timeoutSec = flags [ 'timeout' ] ? parseInt ( flags [ 'timeout' ] as string , 10 ) : 30 ;
230+ const timeoutSec = parseNumericFlag ( flags , 'timeout' , 30 ) ! ;
218231 const timeoutMs = timeoutSec * 1000 ;
219232 const socketTimeout = timeoutMs + 5000 ;
220233
@@ -294,7 +307,7 @@ async function main(): Promise<void> {
294307 }
295308
296309 if ( cmd0 === 'profile' && cmd1 === 'slow' ) {
297- const limit = flags [ 'limit' ] ? parseInt ( flags [ 'limit' ] as string , 10 ) : undefined ;
310+ const limit = parseNumericFlag ( flags , 'limit' ) ;
298311 const resp = await sendCommand ( { type : 'profile-slow' , limit } ) ;
299312 if ( resp . ok ) {
300313 console . log ( formatSlowest ( resp . data as any ) ) ;
@@ -306,7 +319,7 @@ async function main(): Promise<void> {
306319 }
307320
308321 if ( cmd0 === 'profile' && cmd1 === 'rerenders' ) {
309- const limit = flags [ 'limit' ] ? parseInt ( flags [ 'limit' ] as string , 10 ) : undefined ;
322+ const limit = parseNumericFlag ( flags , 'limit' ) ;
310323 const resp = await sendCommand ( { type : 'profile-rerenders' , limit } ) ;
311324 if ( resp . ok ) {
312325 console . log ( formatRerenders ( resp . data as any ) ) ;
@@ -328,7 +341,7 @@ async function main(): Promise<void> {
328341 console . error ( 'Usage: devtools profile commit <N | #N>' ) ;
329342 process . exit ( 1 ) ;
330343 }
331- const limit = flags [ 'limit' ] ? parseInt ( flags [ 'limit' ] as string , 10 ) : undefined ;
344+ const limit = parseNumericFlag ( flags , 'limit' ) ;
332345 const resp = await sendCommand ( { type : 'profile-commit' , index, limit } ) ;
333346 if ( resp . ok ) {
334347 console . log ( formatCommitDetail ( resp . data as any ) ) ;
@@ -340,7 +353,7 @@ async function main(): Promise<void> {
340353 }
341354
342355 if ( cmd0 === 'profile' && cmd1 === 'timeline' ) {
343- const limit = flags [ 'limit' ] ? parseInt ( flags [ 'limit' ] as string , 10 ) : undefined ;
356+ const limit = parseNumericFlag ( flags , 'limit' ) ;
344357 const resp = await sendCommand ( { type : 'profile-timeline' , limit } ) ;
345358 if ( resp . ok ) {
346359 console . log ( formatTimeline ( resp . data as any ) ) ;
0 commit comments