@@ -90,19 +90,19 @@ public static function parseArgv(array $params, array $config = []): array
9090
9191 $ config = \array_merge ([
9292 // List of parameters without values(bool option keys)
93- 'noValues ' => [], // ['debug', 'h']
93+ 'boolOpts ' => [], // ['debug', 'h']
9494 // Whether merge short-opts and long-opts
9595 // 'mergeOpts' => false,
9696 // want parsed options. if not empty, will ignore no matched
9797 'wantParsedOpts ' => [],
98- // list of params allow array.
99- 'arrayValues ' => [], // ['names', 'status']
98+ // list of option allow array values .
99+ 'arrayOpts ' => [], // ['names', 'status']
100100 ], $ config );
101101
102102 $ args = $ sOpts = $ lOpts = [];
103103 // config
104- $ noValues = \array_flip ((array )$ config ['noValues ' ]);
105- $ arrValues = \array_flip ((array )$ config ['arrayValues ' ]);
104+ $ boolOpts = \array_flip ((array )$ config ['boolOpts ' ]);
105+ $ arrayOpts = \array_flip ((array )$ config ['arrayOpts ' ]);
106106
107107 // each() will deprecated at 7.2. so,there use current and next instead it.
108108 // while (list(,$p) = each($params)) {
@@ -111,65 +111,69 @@ public static function parseArgv(array $params, array $config = []): array
111111
112112 // is options
113113 if ($ p {0 } === '- ' ) {
114- $ val = true ;
115- $ opt = \substr ($ p , 1 );
114+ $ value = true ;
115+ $ option = \substr ($ p , 1 );
116116 $ isLong = false ;
117117
118118 // long-opt: (--<opt>)
119- if (\strpos ($ opt , '- ' ) === 0 ) {
120- $ opt = \substr ($ opt , 1 );
119+ if (\strpos ($ option , '- ' ) === 0 ) {
120+ $ option = \substr ($ option , 1 );
121121 $ isLong = true ;
122122
123123 // long-opt: value specified inline (--<opt>=<value>)
124- if (\strpos ($ opt , '= ' ) !== false ) {
125- list ($ opt , $ val ) = \explode ('= ' , $ opt , 2 );
124+ if (\strpos ($ option , '= ' ) !== false ) {
125+ list ($ option , $ value ) = \explode ('= ' , $ option , 2 );
126126 }
127127
128128 // short-opt: value specified inline (-<opt>=<value>)
129- } elseif (isset ($ opt {1 }) && $ opt {1 } === '= ' ) {
130- list ($ opt , $ val ) = \explode ('= ' , $ opt , 2 );
129+ } elseif (isset ($ option {1 }) && $ option {1 } === '= ' ) {
130+ list ($ option , $ value ) = \explode ('= ' , $ option , 2 );
131131 }
132132
133133 // check if next parameter is a descriptor or a value
134134 $ nxt = \current ($ params );
135135
136136 // next elem is value. fix: allow empty string ''
137- if ($ val === true && !isset ($ noValues [ $ opt ]) && self ::nextIsValue ($ nxt )) {
137+ if ($ value === true && !isset ($ boolOpts [ $ option ]) && self ::nextIsValue ($ nxt )) {
138138 // list(,$val) = each($params);
139- $ val = $ nxt ;
139+ $ value = $ nxt ;
140140 \next ($ params );
141141
142142 // short-opt: bool opts. like -e -abc
143- } elseif (!$ isLong && $ val === true ) {
144- foreach (\str_split ($ opt ) as $ char ) {
143+ } elseif (!$ isLong && $ value === true ) {
144+ foreach (\str_split ($ option ) as $ char ) {
145145 $ sOpts [$ char ] = true ;
146146 }
147147 continue ;
148148 }
149149
150- $ val = self ::filterBool ($ val );
151- $ isArray = isset ($ arrValues [ $ opt ]);
150+ $ value = self ::filterBool ($ value );
151+ $ isArray = isset ($ arrayOpts [ $ option ]);
152152
153153 if ($ isLong ) {
154154 if ($ isArray ) {
155- $ lOpts [$ opt ][] = $ val ;
155+ $ lOpts [$ option ][] = $ value ;
156156 } else {
157- $ lOpts [$ opt ] = $ val ;
157+ $ lOpts [$ option ] = $ value ;
158158 }
159159 } elseif ($ isArray ) { // short
160- $ sOpts [$ opt ][] = $ val ;
160+ $ sOpts [$ option ][] = $ value ;
161161 } else { // short
162- $ sOpts [$ opt ] = $ val ;
162+ $ sOpts [$ option ] = $ value ;
163163 }
164- // arguments: param doesn't belong to any option, define it is args
164+
165+ continue ;
166+ }
167+
168+ // parse arguments:
169+ // - param doesn't belong to any option, define it is args
170+
171+ // value specified inline (<arg>=<value>)
172+ if (\strpos ($ p , '= ' ) !== false ) {
173+ list ($ name , $ value ) = \explode ('= ' , $ p , 2 );
174+ $ args [$ name ] = self ::filterBool ($ value );
165175 } else {
166- // value specified inline (<arg>=<value>)
167- if (\strpos ($ p , '= ' ) !== false ) {
168- list ($ name , $ val ) = \explode ('= ' , $ p , 2 );
169- $ args [$ name ] = self ::filterBool ($ val );
170- } else {
171- $ args [] = $ p ;
172- }
176+ $ args [] = $ p ;
173177 }
174178 }
175179
@@ -183,6 +187,7 @@ public static function parseArgv(array $params, array $config = []): array
183187 * 'arg' => 'val',
184188 * '--lp' => 'val2',
185189 * '--s' => 'val3',
190+ * '-h' => true,
186191 * ]);
187192 * ```
188193 * @param array $params
@@ -193,14 +198,22 @@ public static function parseArray(array $params): array
193198 $ args = $ sOpts = $ lOpts = [];
194199
195200 foreach ($ params as $ key => $ val ) {
196- if ($ key === '-- ' || $ key === '- ' ) {
201+ if (\is_int ($ key )) { // as argument
202+ $ args [$ key ] = $ val ;
197203 continue ;
198204 }
199205
200- if (0 === \strpos ($ key , '-- ' )) {
201- $ lOpts [substr ($ key , 2 )] = $ val ;
202- } elseif (\strpos ($ key , '- ' ) === 0 ) {
203- $ sOpts [\substr ($ key , 1 )] = $ val ;
206+ $ cleanKey = \trim ((string )$ key , '- ' );
207+
208+ if ('' === $ cleanKey ) { // as argument
209+ $ args [] = $ val ;
210+ continue ;
211+ }
212+
213+ if (0 === \strpos ($ key , '-- ' )) { // long option
214+ $ lOpts [$ cleanKey ] = $ val ;
215+ } elseif (0 === \strpos ($ key , '- ' )) { // short option
216+ $ sOpts [$ cleanKey ] = $ val ;
204217 } else {
205218 $ args [$ key ] = $ val ;
206219 }
@@ -210,9 +223,12 @@ public static function parseArray(array $params): array
210223 }
211224
212225 /**
226+ * parse flags from a string
227+ *
213228 * ```php
214229 * $result = Flags::parseString('foo --bar="foobar"');
215230 * ```
231+ *
216232 * @todo ...
217233 * @param string $string
218234 */
@@ -250,7 +266,7 @@ public static function filterBool($val, $enable = true)
250266 * @param mixed $val
251267 * @return bool
252268 */
253- public static function nextIsValue (string $ val ): bool
269+ public static function nextIsValue ($ val ): bool
254270 {
255271 // current() fetch error, will return FALSE
256272 if ($ val === false ) {
0 commit comments