@@ -41,13 +41,23 @@ final class CommandLine
4141 * --long-opt=<value>
4242 * @link http://php.net/manual/zh/function.getopt.php#83414
4343 * @param array $params
44- * @param array $noValues List of parameters without values(bool option keys) noVal
45- * @param bool $mergeOpts Whether merge short-opts and long-opts
44+ * @param array $config
4645 * @return array
4746 */
48- public static function parseByArgv (array $ params , array $ noValues = [], $ mergeOpts = false ): array
47+ public static function parseByArgv (array $ params , array $ config = []): array
4948 {
49+ $ config = array_merge ([
50+ // List of parameters without values(bool option keys)
51+ 'noValues ' => [], // ['debug', 'h']
52+ // Whether merge short-opts and long-opts
53+ 'mergeOpts ' => false ,
54+ // list of params allow array.
55+ 'arrayValues ' => [], // ['names', 'status']
56+ ], $ config );
57+
5058 $ args = $ sOpts = $ lOpts = [];
59+ $ noValues = array_flip ((array )$ config ['noValues ' ]);
60+ $ arrayValues = array_flip ((array )$ config ['arrayValues ' ]);
5161
5262 // each() will deprecated at 7.2. so,there use current and next instead it.
5363 // while (list(,$p) = each($params)) {
@@ -79,7 +89,7 @@ public static function parseByArgv(array $params, array $noValues = [], $mergeOp
7989 $ nxt = current ($ params );
8090
8191 // next elem is value. fix: allow empty string ''
82- if ($ val === true && self ::nextIsValue ($ nxt ) && !\in_array ( $ opt , $ noValues, true )) {
92+ if ($ val === true && self ::nextIsValue ($ nxt ) && !isset ( $ noValues[ $ opt ] )) {
8393 // list(,$val) = each($params);
8494 $ val = $ nxt ;
8595 next ($ params );
@@ -93,10 +103,21 @@ public static function parseByArgv(array $params, array $noValues = [], $mergeOp
93103 continue ;
94104 }
95105
106+ $ val = self ::filterBool ($ val );
107+ $ isArray = isset ($ arrayValues [$ opt ]);
108+
96109 if ($ isLong ) {
97- $ lOpts [$ opt ] = self ::filterBool ($ val );
110+ if ($ isArray ) {
111+ $ lOpts [$ opt ][] = $ val ;
112+ } else {
113+ $ lOpts [$ opt ] = $ val ;
114+ }
98115 } else {
99- $ sOpts [$ opt ] = self ::filterBool ($ val );
116+ if ($ isArray ) {
117+ $ sOpts [$ opt ][] = $ val ;
118+ } else {
119+ $ sOpts [$ opt ] = $ val ;
120+ }
100121 }
101122
102123 // arguments: param doesn't belong to any option, define it is args
@@ -111,7 +132,7 @@ public static function parseByArgv(array $params, array $noValues = [], $mergeOp
111132 }
112133 }
113134
114- if ($ mergeOpts ) {
135+ if ($ config [ ' mergeOpts ' ] ) {
115136 return [$ args , array_merge ($ sOpts , $ lOpts )];
116137 }
117138
0 commit comments