1 | <?php |
||
11 | class ArgumentParser |
||
12 | { |
||
13 | private $description; |
||
14 | |||
15 | private $footer; |
||
16 | |||
17 | private $name; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $optionsCache = []; |
||
23 | |||
24 | private $options = []; |
||
25 | |||
26 | /** |
||
27 | * @var HelpMessageGenerator |
||
28 | */ |
||
29 | private $helpGenerator; |
||
30 | |||
31 | 7 | public function __construct($helpWriter = null) |
|
35 | |||
36 | /** |
||
37 | * Add a value to the available possible options for later parsing. |
||
38 | * |
||
39 | * @param $key |
||
40 | * @param $value |
||
41 | * @throws OptionExistsException |
||
42 | */ |
||
43 | 6 | private function addToOptionCache($key, $value) |
|
51 | |||
52 | /** |
||
53 | * Add an option to be parsed. |
||
54 | * Arguments are presented as a structured array with the following possible keys. |
||
55 | * |
||
56 | * name: The name of the option prefixed with a double dash -- |
||
57 | * short_name: A shorter single character option prefixed with a single dash - |
||
58 | * type: Required for all options that take values. An option specified without a type is considered to be a |
||
59 | * boolean flag. |
||
60 | * help: A help message for the option |
||
61 | * |
||
62 | * @param $option |
||
63 | * @throws OptionExistsException |
||
64 | * @throws InvalidArgumentDescriptionException |
||
65 | */ |
||
66 | 7 | public function addOption($option) |
|
75 | |||
76 | /** |
||
77 | * @param $arguments |
||
78 | * @param $argPointer |
||
79 | * @return mixed |
||
80 | * @throws InvalidValueException |
||
81 | */ |
||
82 | 3 | private function getNextValueOrFail($arguments, &$argPointer, $name) |
|
91 | |||
92 | /** |
||
93 | * Parse a long argument that is prefixed with a double dash "--" |
||
94 | * |
||
95 | * @param $arguments |
||
96 | * @param $argPointer |
||
97 | * @return array |
||
98 | * @throws InvalidValueException |
||
99 | */ |
||
100 | 3 | private function parseLongArgument($arguments, &$argPointer) |
|
118 | |||
119 | /** |
||
120 | * Parse a short argument that is prefixed with a single dash '-' |
||
121 | * |
||
122 | * @param $arguments |
||
123 | * @param $argPointer |
||
124 | * @return array |
||
125 | * @throws InvalidValueException |
||
126 | */ |
||
127 | 2 | public function parseShortArgument($arguments, &$argPointer) |
|
144 | |||
145 | 2 | private function castType($value, $type) |
|
153 | |||
154 | /** |
||
155 | * @param $arguments |
||
156 | * @return array |
||
157 | * @throws InvalidValueException |
||
158 | */ |
||
159 | 4 | private function parseArgumentArray($arguments) |
|
177 | |||
178 | 2 | private function maybeShowHelp($name, $output) |
|
184 | |||
185 | /** |
||
186 | * Parses command line arguments and return a structured array of options and their associated values. |
||
187 | * |
||
188 | * @param array $arguments An optional array of arguments that would be parsed instead of those passed to the CLI. |
||
189 | * @return array |
||
190 | * @throws InvalidValueException |
||
191 | */ |
||
192 | 4 | public function parse($arguments = null) |
|
200 | |||
201 | /** |
||
202 | * @param $name |
||
203 | * @param null $description |
||
204 | * @param null $footer |
||
205 | * @throws InvalidArgumentDescriptionException |
||
206 | * @throws OptionExistsException |
||
207 | */ |
||
208 | 1 | public function enableHelp($name, $description=null, $footer=null) |
|
216 | } |
||
217 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.