99
1010namespace Toolkit \Cli ;
1111
12+ use Toolkit \Cli \Util \LineParser ;
1213use function array_flip ;
1314use function array_merge ;
1415use function current ;
2122use function preg_match ;
2223use function str_split ;
2324use function stripos ;
24- use function strlen ;
2525use function strpos ;
2626use function substr ;
2727use function trim ;
@@ -77,35 +77,43 @@ public static function simpleParseArgv(array $argv): array
7777 * Parses $GLOBALS['argv'] for parameters and assigns them to an array.
7878 * eg:
7979 *
80- * ```
80+ * ```bash
8181 * php cli.php run name=john city=chengdu -s=test --page=23 -d -rf --debug --task=off -y=false -D -e dev -v vvv
8282 * ```
8383 *
84+ * Usage:
85+ *
8486 * ```php
8587 * $argv = $_SERVER['argv'];
8688 * // notice: must shift first element.
8789 * $script = \array_shift($argv);
8890 * $result = Flags::parseArgv($argv);
8991 * ```
9092 *
91- * Supports args:
93+ * Supports args style:
94+ *
95+ * ```bash
9296 * <value>
9397 * arg=<value>
94- * Supports opts:
98+ * ```
99+ *
100+ * Supports opts style:
101+ *
102+ * ```bash
95103 * -e
96104 * -e <value>
97105 * -e=<value>
98106 * --long-opt
99107 * --long-opt <value>
100108 * --long-opt=<value>
109+ * ```
101110 *
102111 * @link http://php.net/manual/zh/function.getopt.php#83414
103112 *
104113 * @param array $params
105114 * @param array $config
106115 *
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]`
109117 */
110118 public static function parseArgv (array $ params , array $ config = []): array
111119 {
@@ -118,10 +126,15 @@ public static function parseArgv(array $params, array $config = []): array
118126 'boolOpts ' => [], // ['debug', 'h']
119127 // Whether merge short-opts and long-opts
120128 '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
122131 'wantParsedOpts ' => [],
123- // list of option allow array values.
132+ // List of option allow array values.
124133 '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 ' ,
125138 ], $ config );
126139
127140 $ args = $ sOpts = $ lOpts = [];
@@ -142,8 +155,8 @@ public static function parseArgv(array $params, array $config = []): array
142155 // is options and not equals '-' '--'
143156 if ($ p [0 ] === '- ' && '' !== trim ($ p , '- ' )) {
144157 $ value = true ;
145- $ option = substr ($ p , 1 );
146158 $ isLong = false ;
159+ $ option = substr ($ p , 1 );
147160
148161 // long-opt: (--<opt>)
149162 if (strpos ($ option , '- ' ) === 0 ) {
@@ -264,25 +277,29 @@ public static function parseArray(array $params): array
264277 }
265278
266279 /**
267- * parse flags from a string
280+ * Parse flags from a string
268281 *
269282 * ```php
270283 * $result = Flags::parseString('foo --bar="foobar"');
271284 * ```
272285 *
273286 * @param string $string
287+ * @param array $config
274288 *
275- * @todo ...
289+ * @return array
276290 */
277- public static function parseString (string $ string ): void
291+ public static function parseString (string $ string, array $ config = [] ): array
278292 {
293+ $ flags = LineParser::parseIt ($ string );
294+
295+ return self ::parseArgv ($ flags , $ config );
279296 }
280297
281298 /**
282299 * @param string|bool $val
283300 * @param bool $enable
284301 *
285- * @return bool|mixed
302+ * @return bool|int| mixed
286303 */
287304 public static function filterBool ($ val , bool $ enable = true )
288305 {
0 commit comments