Skip to content

Commit 7953a60

Browse files
committed
some update for params parse. add 'arrayValues' option
1 parent 34077f2 commit 7953a60

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

examples/Controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ public function jsonCommand()
611611
* a example for use arguments on command
612612
* @usage home:useArg [arg1=val1 arg2=arg2] [options]
613613
* @example
614-
* home:useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false -a v1 --ab -c -g --cd val -h ''
614+
* home:useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false -a v1 --ab -c -g --cd val -h '' -i stat=online
615615
* home:useArg status=2 name=john name=tom name=jack arg0 -s=test --page=23 --id=23 --id=154 --id=456 -d -rf --debug --test=false
616616
*/
617617
public function useArgCommand()

examples/app

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $app->setLogo("
2020
/_/ /_/
2121
", 'success');
2222

23-
require __DIR__ . '/routes.php';
23+
require __DIR__ . '/commands.php';
2424

2525
// run
2626
$app->run();
File renamed without changes.

src/Utils/CommandLine.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)