@@ -37,7 +37,7 @@ const set = (obj: NestedMapping, key: string, value: any, type?: string) => {
3737
3838const type = (
3939 key : string ,
40- opts : Record < "boolean" | "string" | "array" , string [ ] >
40+ opts : Record < "boolean" | "string" | "array" , string [ ] > ,
4141) : "boolean" | "string" | "array" | undefined => {
4242 if ( opts . array && opts . array . length > 0 && opts . array . includes ( key ) )
4343 return "array" ;
@@ -56,12 +56,13 @@ const defaultValue = (type?: "boolean" | "string" | "array") => {
5656
5757const coerce = ( value ?: string , type ?: "string" | "boolean" | "array" ) => {
5858 if ( type === "string" ) return value ;
59- if ( type === "boolean" ) return value === undefined ? true : value === ' true' ;
59+ if ( type === "boolean" ) return value === undefined ? true : value === " true" ;
6060
6161 if ( ! value ) return value ;
6262 if ( value . length > 3 && BOOL_RE . test ( value ) ) return value === "true" ;
6363 if ( value . length > 2 && QUOTED_RE . test ( value ) ) return value . slice ( 1 , - 1 ) ;
64- if ( value [ 0 ] === '.' && / \d / . test ( value [ 1 ] ) || / \d / . test ( value [ 0 ] ) ) return Number ( value ) ;
64+ if ( ( value [ 0 ] === "." && / \d / . test ( value [ 1 ] ) ) || / \d / . test ( value [ 0 ] ) )
65+ return Number ( value ) ;
6566 return value ;
6667} ;
6768
@@ -80,14 +81,14 @@ export function parse<
8081 TDefaults extends Record < string , unknown > | undefined = undefined ,
8182 TAliases extends Aliases < TAliasArgNames , TAliasNames > | undefined = undefined ,
8283 TAliasArgNames extends string = string ,
83- TAliasNames extends string = string
84+ TAliasNames extends string = string ,
8485> (
8586 argv : string [ ] ,
8687 {
8788 default : defaults ,
8889 alias : aliases ,
8990 ...types
90- } : ParseOptions < TBooleans , TStrings , TCollectable , TDefaults , TAliases > = { }
91+ } : ParseOptions < TBooleans , TStrings , TCollectable , TDefaults , TAliases > = { } ,
9192) : Args < TArgs > {
9293 const obj = { ...defaults , _ : [ ] } as unknown as Args < TArgs > ;
9394 if ( argv . length === 0 ) return obj ;
@@ -96,57 +97,60 @@ export function parse<
9697 const curr = argv [ i ] ;
9798 const next = argv [ i + 1 ] ;
9899
99- let t : ' string' | ' boolean' | ' array' | undefined ;
100- let key = '' ;
100+ let t : " string" | " boolean" | " array" | undefined ;
101+ let key = "" ;
101102 let value : string | undefined ;
102103
103104 if ( curr . length > 1 && curr [ 0 ] === "-" ) {
104- if ( curr [ 1 ] !== "-" && curr . length > 2 && ! curr . includes ( '=' ) ) {
105- if ( curr . includes ( '.' ) ) {
105+ if ( curr [ 1 ] !== "-" && curr . length > 2 && ! curr . includes ( "=" ) ) {
106+ if ( curr . includes ( "." ) ) {
106107 key = curr . slice ( 1 , 2 ) ;
107108 value = curr . slice ( 2 ) ;
108109 } else {
109110 const keys = curr . slice ( 1 , - 1 ) ;
110111 for ( let key of keys ) {
111- if ( aliases && ( aliases as Record < string , any > ) [ key ] !== undefined ) {
112+ if (
113+ aliases &&
114+ ( aliases as Record < string , any > ) [ key ] !== undefined
115+ ) {
112116 key = aliases [ key as keyof typeof aliases ] as string ;
113117 }
114- set ( obj , key , defaultValue ( t ) , t )
118+ set ( obj , key , defaultValue ( t ) , t ) ;
115119 }
116- key = curr . slice ( - 1 )
117- if ( next && next [ 0 ] !== '-' ) {
120+ key = curr . slice ( - 1 ) ;
121+ if ( next && next [ 0 ] !== "-" ) {
118122 value = next ;
119123 i ++ ;
120124 }
121125 }
122126 } else if ( ! curr . includes ( "=" ) && next && next [ 0 ] !== "-" ) {
123- key = curr . replace ( / ^ - { 1 , 2 } / , '' ) ;
127+ key = curr . replace ( / ^ - { 1 , 2 } / , "" ) ;
124128 t = type ( key , types as any ) ;
125129 // treat boolean as flag without parameter
126- if ( t === ' boolean' ) {
127- value = ' true' ;
130+ if ( t === " boolean" ) {
131+ value = " true" ;
128132 } else {
129133 value = next ;
130134 i ++ ;
131135 }
132136 } else {
133- const eq = curr . indexOf ( '=' ) ;
137+ const eq = curr . indexOf ( "=" ) ;
134138 if ( eq === - 1 ) {
135- key = curr . replace ( / ^ - { 1 , 2 } / , '' ) ;
139+ key = curr . replace ( / ^ - { 1 , 2 } / , "" ) ;
136140 } else {
137- key = curr . slice ( 0 , eq ) . replace ( / ^ - { 1 , 2 } / , '' ) ;
141+ key = curr . slice ( 0 , eq ) . replace ( / ^ - { 1 , 2 } / , "" ) ;
138142 value = curr . slice ( eq + 1 ) ;
139143 }
140144 t = type ( key , types as any ) ;
141145 }
142146
143- if ( ( ! t || t === "boolean" ) && key . length > 3 && key . startsWith ( ' no-' ) ) {
144- set ( obj , key . slice ( 3 ) , false )
147+ if ( ( ! t || t === "boolean" ) && key . length > 3 && key . startsWith ( " no-" ) ) {
148+ set ( obj , key . slice ( 3 ) , false ) ;
145149 } else {
146150 if ( aliases && ( aliases as Record < string , any > ) [ key ] !== undefined ) {
147151 key = aliases [ key as keyof typeof aliases ] as string ;
148152 }
149- set ( obj , key , coerce ( value , t ) ?? defaultValue ( t ) , t )
153+ set ( obj , key , coerce ( value , t ) ?? defaultValue ( t ) , t ) ;
150154 }
151155 } else if ( curr ) {
152156 ( obj as any ) . _ . push ( coerce ( curr ) ) ;
0 commit comments