@@ -47,6 +47,21 @@ function getMainArgs() {
4747 return ArrayPrototypeSlice ( process . argv , 2 ) ;
4848}
4949
50+ function storeOptionValue ( parseOptions , option , value , result ) {
51+ result . flags [ option ] = true ;
52+
53+ // Append value to previous values array for case of multiples
54+ // option, else add to empty array
55+ result . values [ option ] = ArrayPrototypeConcat (
56+ [ ] ,
57+ parseOptions . multiples &&
58+ ArrayPrototypeIncludes ( parseOptions . multiples , option ) &&
59+ result . values [ option ] ||
60+ [ ] ,
61+ value
62+ ) ;
63+ }
64+
5065const parseArgs = (
5166 argv = getMainArgs ( ) ,
5267 options = { }
@@ -89,59 +104,31 @@ const parseArgs = (
89104 // withValue equals(=) case
90105 const argParts = StringPrototypeSplit ( arg , '=' ) ;
91106
92- result . flags [ argParts [ 0 ] ] = true ;
93107 // If withValue option is specified, take 2nd part after '=' as value,
94108 // else set value as undefined
95109 const val = options . withValue &&
96110 ArrayPrototypeIncludes ( options . withValue , argParts [ 0 ] ) ?
97111 argParts [ 1 ] : undefined ;
98- // Append value to previous values array for case of multiples
99- // option, else add to empty array
100- result . values [ argParts [ 0 ] ] = ArrayPrototypeConcat ( [ ] ,
101- options . multiples &&
102- ArrayPrototypeIncludes ( options . multiples , argParts [ 0 ] ) &&
103- result . values [ argParts [ 0 ] ] || [ ] ,
104- val ,
105- ) ;
112+ storeOptionValue ( options , argParts [ 0 ] , val , result ) ;
106113 } else if ( pos + 1 < argv . length &&
107114 ! StringPrototypeStartsWith ( argv [ pos + 1 ] , '-' )
108115 ) {
109116 // withValue option should also support setting values when '=
110117 // isn't used ie. both --foo=b and --foo b should work
111118
112- result . flags [ arg ] = true ;
113119 // If withValue option is specified, take next position arguement as
114120 // value and then increment pos so that we don't re-evaluate that
115121 // arg, else set value as undefined ie. --foo b --bar c, after setting
116122 // b as the value for foo, evaluate --bar next and skip 'b'
117123 const val = options . withValue &&
118124 ArrayPrototypeIncludes ( options . withValue , arg ) ? argv [ ++ pos ] :
119125 undefined ;
120- // Append value to previous values array for case of multiples
121- // option, else add to empty array
122- result . values [ arg ] = ArrayPrototypeConcat (
123- [ ] ,
124- options . multiples &&
125- ArrayPrototypeIncludes ( options . multiples , arg ) &&
126- result . values [ arg ] ||
127- [ ] ,
128- val
129- ) ;
126+ storeOptionValue ( options , arg , val , result ) ;
130127 } else {
131128 // Cases when an arg is specified without a value, example
132129 // '--foo --bar' <- 'foo' and 'bar' flags should be set to true and
133130 // shave value as undefined
134- result . flags [ arg ] = true ;
135- // Append undefined to previous values array for case of
136- // multiples option, else add to empty array
137- result . values [ arg ] = ArrayPrototypeConcat (
138- [ ] ,
139- options . multiples &&
140- ArrayPrototypeIncludes ( options . multiples , arg ) &&
141- result . values [ arg ] ||
142- [ ] ,
143- undefined
144- ) ;
131+ storeOptionValue ( options , arg , undefined , result ) ;
145132 }
146133
147134 } else {
0 commit comments