|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace inhere\console\examples\baks; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by PhpStorm. |
| 7 | + * User: inhere |
| 8 | + * Date: 2017/5/29 |
| 9 | + * Time: 下午10:16 |
| 10 | + */ |
| 11 | +class OldInput |
| 12 | +{ |
| 13 | + |
| 14 | + ///////////////////////////////////////////////////////////////////////////////////////// |
| 15 | + /// argument and option parser |
| 16 | + ///////////////////////////////////////////////////////////////////////////////////////// |
| 17 | + |
| 18 | + /** |
| 19 | + * @param bool $fillToGlobal |
| 20 | + * @return array |
| 21 | + */ |
| 22 | + public static function parseGlobalArgv($fillToGlobal = false) |
| 23 | + { |
| 24 | + // eg: `./bin/app server name=john city -s=test --page=23 -d -rf --debug` |
| 25 | + // eg: `php cli.php server name=john city -s=test --page=23 -d -rf --debug` |
| 26 | + global $argv; |
| 27 | + |
| 28 | + $tmp = $argv; |
| 29 | + $fullScript = implode(' ', $tmp); |
| 30 | + $script = array_shift($tmp); |
| 31 | + |
| 32 | + $args = $opts = []; |
| 33 | + |
| 34 | + // parse query params |
| 35 | + // `./bin/app server start name=john city=chengdu -s=test --page=23 -d -rf --debug --task=off` |
| 36 | + // parse to |
| 37 | + // $args = [ 'name' => 'john', 0 => 'start', 'city' => 'chengdu' ]; |
| 38 | + // $opts = [ 'd' => true, 'f' => true, 'r' => true, 's' => 'test', 'debug' => true, 'task' => false ] |
| 39 | + if ($tmp) { |
| 40 | + foreach ($tmp as $item) { |
| 41 | + // is a option |
| 42 | + if ($item{0} === '-') { |
| 43 | + static::parseOption($item, $opts); |
| 44 | + |
| 45 | + // is a argument |
| 46 | + } else { |
| 47 | + static::parseArgument($item, $args); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if ($fillToGlobal) { |
| 52 | + $_REQUEST = $_GET = $args; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // collect command `server` |
| 57 | + $command = isset($args[0]) ? array_shift($args) : ''; |
| 58 | + |
| 59 | + unset($tmp); |
| 60 | + return [$fullScript, $script, $command, $args, $opts]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * will parse option |
| 65 | + * |
| 66 | + * eg: `-s=test --page=23 -d -rf --debug --task=false --id=23 --id=154` |
| 67 | + * |
| 68 | + * to: |
| 69 | + * |
| 70 | + * ``` |
| 71 | + * $opts = [ |
| 72 | + * 'd' => true, |
| 73 | + * 'f' => true, |
| 74 | + * 'r' => true, |
| 75 | + * 's' => 'test', |
| 76 | + * 'debug' => true, |
| 77 | + * 'task' => false |
| 78 | + * ] |
| 79 | + * ``` |
| 80 | + * @param $item |
| 81 | + * @param $opts |
| 82 | + */ |
| 83 | + protected static function parseOption($item, &$opts) |
| 84 | + { |
| 85 | + // is a have value option. eg: `-s=test --page=23` |
| 86 | + if (strpos($item, '=')) { |
| 87 | + $item = trim($item, '-= '); |
| 88 | + [$name, $val] = explode('=', $item); |
| 89 | + $tVal = strtolower($val); |
| 90 | + |
| 91 | + // check it is a bool value. |
| 92 | + if ($tVal === 'on' || $tVal === 'yes' || $tVal === 'true') { |
| 93 | + $opts[$name] = true; |
| 94 | + } elseif ($tVal === 'off' || $tVal === 'no' || $tVal === 'false') { |
| 95 | + $opts[$name] = false; |
| 96 | + |
| 97 | + // is array. eg: `--id=23 --id=154` |
| 98 | + } elseif (isset($opts[$name])) { |
| 99 | + if (is_array($opts[$name])) { |
| 100 | + $opts[$name][] = $val; |
| 101 | + |
| 102 | + // expect bool option. so not use `else` |
| 103 | + } elseif (is_string($opts[$name])) { |
| 104 | + $prev = $opts[$name]; |
| 105 | + $opts[$name] = [$prev, $val]; |
| 106 | + } |
| 107 | + } else { |
| 108 | + $opts[$name] = $val; |
| 109 | + } |
| 110 | + |
| 111 | + // is a no value option |
| 112 | + } else { |
| 113 | + // is a short option. eg: `-d -rf` |
| 114 | + if ($item{1} !== '-') { |
| 115 | + $item = trim($item, '-'); |
| 116 | + foreach (str_split($item) as $char) { |
| 117 | + $opts[$char] = true; |
| 118 | + } |
| 119 | + |
| 120 | + // is a long option. eg: `--debug` |
| 121 | + } else { |
| 122 | + $item = trim($item, '-'); |
| 123 | + $opts[$item] = true; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * parse argument list |
| 130 | + * |
| 131 | + * eg: `start name=john name=tom city=chengdu` |
| 132 | + * |
| 133 | + * to: |
| 134 | + * |
| 135 | + * ``` |
| 136 | + * [ 'name' => ['john', 'tom'], 0 => 'start', 'city' => 'chengdu' ]; |
| 137 | + * ``` |
| 138 | + * |
| 139 | + * @param $item |
| 140 | + * @param array $args |
| 141 | + */ |
| 142 | + protected static function parseArgument($item, &$args) |
| 143 | + { |
| 144 | + $item = trim($item, '= '); |
| 145 | + |
| 146 | + // eg: `name=john` |
| 147 | + if (strpos($item, '=')) { |
| 148 | + [$name, $val] = explode('=', $item); |
| 149 | + |
| 150 | + // is array. eg: `name=john name=tom` |
| 151 | + if (isset($args[$name])) { |
| 152 | + if (is_array($args[$name])) { |
| 153 | + $args[$name][] = $val; |
| 154 | + } else { |
| 155 | + $prev = $args[$name]; |
| 156 | + $args[$name] = [$prev, $val]; |
| 157 | + } |
| 158 | + } else { |
| 159 | + $args[$name] = $val; |
| 160 | + } |
| 161 | + |
| 162 | + // only value. eg: `city` |
| 163 | + } else { |
| 164 | + $args[] = $item; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments