9
9
10
10
namespace Toolkit \Cli ;
11
11
12
+ use Toolkit \Cli \Util \LineParser ;
12
13
use function array_flip ;
13
14
use function array_merge ;
14
15
use function current ;
21
22
use function preg_match ;
22
23
use function str_split ;
23
24
use function stripos ;
24
- use function strlen ;
25
25
use function strpos ;
26
26
use function substr ;
27
27
use function trim ;
@@ -77,35 +77,43 @@ public static function simpleParseArgv(array $argv): array
77
77
* Parses $GLOBALS['argv'] for parameters and assigns them to an array.
78
78
* eg:
79
79
*
80
- * ```
80
+ * ```bash
81
81
* php cli.php run name=john city=chengdu -s=test --page=23 -d -rf --debug --task=off -y=false -D -e dev -v vvv
82
82
* ```
83
83
*
84
+ * Usage:
85
+ *
84
86
* ```php
85
87
* $argv = $_SERVER['argv'];
86
88
* // notice: must shift first element.
87
89
* $script = \array_shift($argv);
88
90
* $result = Flags::parseArgv($argv);
89
91
* ```
90
92
*
91
- * Supports args:
93
+ * Supports args style:
94
+ *
95
+ * ```bash
92
96
* <value>
93
97
* arg=<value>
94
- * Supports opts:
98
+ * ```
99
+ *
100
+ * Supports opts style:
101
+ *
102
+ * ```bash
95
103
* -e
96
104
* -e <value>
97
105
* -e=<value>
98
106
* --long-opt
99
107
* --long-opt <value>
100
108
* --long-opt=<value>
109
+ * ```
101
110
*
102
111
* @link http://php.net/manual/zh/function.getopt.php#83414
103
112
*
104
113
* @param array $params
105
114
* @param array $config
106
115
*
107
- * @return array [args, short-opts, long-opts]
108
- * If 'mergeOpts' is True, will return [args, opts]
116
+ * @return array returns like `[args, short-opts, long-opts]`; If 'mergeOpts' is True, will return `[args, opts]`
109
117
*/
110
118
public static function parseArgv (array $ params , array $ config = []): array
111
119
{
@@ -118,10 +126,15 @@ public static function parseArgv(array $params, array $config = []): array
118
126
'boolOpts ' => [], // ['debug', 'h']
119
127
// Whether merge short-opts and long-opts
120
128
'mergeOpts ' => false ,
121
- // want parsed options. if not empty, will ignore no matched
129
+ // Only want parsed options.
130
+ // if not empty, will ignore no matched
122
131
'wantParsedOpts ' => [],
123
- // list of option allow array values.
132
+ // List of option allow array values.
124
133
'arrayOpts ' => [], // ['names', 'status']
134
+ // Special short style
135
+ // posix: -abc will expand: -a -b -c
136
+ // unix: -abc will expand: -a=bc
137
+ 'shortStyle ' => 'posix ' ,
125
138
], $ config );
126
139
127
140
$ args = $ sOpts = $ lOpts = [];
@@ -142,8 +155,8 @@ public static function parseArgv(array $params, array $config = []): array
142
155
// is options and not equals '-' '--'
143
156
if ($ p [0 ] === '- ' && '' !== trim ($ p , '- ' )) {
144
157
$ value = true ;
145
- $ option = substr ($ p , 1 );
146
158
$ isLong = false ;
159
+ $ option = substr ($ p , 1 );
147
160
148
161
// long-opt: (--<opt>)
149
162
if (strpos ($ option , '- ' ) === 0 ) {
@@ -264,25 +277,29 @@ public static function parseArray(array $params): array
264
277
}
265
278
266
279
/**
267
- * parse flags from a string
280
+ * Parse flags from a string
268
281
*
269
282
* ```php
270
283
* $result = Flags::parseString('foo --bar="foobar"');
271
284
* ```
272
285
*
273
286
* @param string $string
287
+ * @param array $config
274
288
*
275
- * @todo ...
289
+ * @return array
276
290
*/
277
- public static function parseString (string $ string ): void
291
+ public static function parseString (string $ string, array $ config = [] ): array
278
292
{
293
+ $ flags = LineParser::parseIt ($ string );
294
+
295
+ return self ::parseArgv ($ flags , $ config );
279
296
}
280
297
281
298
/**
282
299
* @param string|bool $val
283
300
* @param bool $enable
284
301
*
285
- * @return bool|mixed
302
+ * @return bool|int| mixed
286
303
*/
287
304
public static function filterBool ($ val , bool $ enable = true )
288
305
{
0 commit comments