Total Complexity | 10 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | class Argv |
||
13 | { |
||
14 | /** |
||
15 | * @var Argv |
||
16 | */ |
||
17 | protected static $instance; |
||
18 | |||
19 | /** |
||
20 | * @var array 所有命令行传参 |
||
21 | */ |
||
22 | public $allArgvs = []; |
||
23 | |||
24 | public function __construct() |
||
25 | { |
||
26 | if ($this->get('help') || $this->get('h')) { |
||
27 | $desc = <<<FLL |
||
28 | Description |
||
29 | Params: |
||
30 | -c: <string> 指定要实例化的类名。默认调用FreeNom类 |
||
31 | -m: <string> 指定要调用的方法名(不支持静态方法)。默认调用handle方法 |
||
32 | -h: 显示说明 |
||
33 | |||
34 | Example: |
||
35 | $ php run -c=FreeNom -m=handle |
||
36 | |||
37 | FLL; |
||
38 | echo $desc; |
||
39 | exit(0); |
||
|
|||
40 | } |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @return Argv |
||
45 | */ |
||
46 | public static function instance() |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * 获取命令行参数 |
||
57 | * |
||
58 | * @param string $name |
||
59 | * @param string $default |
||
60 | * |
||
61 | * @return mixed|string |
||
62 | */ |
||
63 | public function get(string $name, string $default = '') |
||
64 | { |
||
65 | if (!$this->allArgvs) { |
||
66 | $this->setAllArgvs(); |
||
67 | } |
||
68 | |||
69 | return $this->allArgvs[$name] ?? $default; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * 设置命令行所有参数 |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function setAllArgvs() |
||
88 | } |
||
89 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.