1 | <?php |
||
18 | class ArgumentsParser |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Collection of CLI parsed "commands" eg. (reset). |
||
23 | * @var Collection |
||
24 | */ |
||
25 | private $commands; |
||
26 | |||
27 | /** |
||
28 | * Collection of CLI parsed "options" eg. (--option=something or --option="something"). |
||
29 | * @var Collection |
||
30 | */ |
||
31 | private $options; |
||
32 | |||
33 | /** |
||
34 | * Collection of CLI parsed "flags" or serial of 'flags' eg. (-y or -yRf). |
||
35 | * @var Collection |
||
36 | */ |
||
37 | private $flags; |
||
38 | |||
39 | /** |
||
40 | * Collection of CLI parsed "arguments" eg. (). |
||
41 | * @var Collection |
||
42 | */ |
||
43 | private $arguments; |
||
44 | |||
45 | /** |
||
46 | * Instaniate with the argument array |
||
47 | * @param array $arguments The CLI arguments array ($argv) to parse. |
||
48 | */ |
||
49 | 56 | public function __construct(array $arguments = []) |
|
58 | |||
59 | /** |
||
60 | * Return the collection of commands (eg. help). |
||
61 | * @return Collection |
||
62 | */ |
||
63 | 8 | public function commands() |
|
67 | |||
68 | /** |
||
69 | * Return the collection of options (eg. --username=bobby). |
||
70 | * @return Collection |
||
71 | */ |
||
72 | 4 | public function options() |
|
73 | { |
||
74 | 4 | return $this->options; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Return the collection of flags (eg. -v or --pass). |
||
79 | * @return Collection |
||
80 | */ |
||
81 | 4 | public function flags() |
|
82 | { |
||
83 | 4 | return $this->flags; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Return all CLI arguments in order. |
||
88 | * @return Collection |
||
89 | */ |
||
90 | 6 | public function arguments() |
|
91 | { |
||
92 | 6 | return $this->arguments; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Parses the PHP $argv variable and adds them to the relivant type collection. |
||
97 | * @todo Clean up the nested loops etc. |
||
98 | * @param array $args |
||
99 | * @return void |
||
100 | */ |
||
101 | 56 | private function parse($args) |
|
117 | |||
118 | /** |
||
119 | * Checks to see if a flag or serial flag (eg. -y or --pass) is set |
||
120 | * @param string $flag The flag name/character (can be single or a serial character) |
||
121 | * @return boolean |
||
122 | */ |
||
123 | 2 | public function isFlagSet($flag) |
|
124 | { |
||
125 | 2 | if (in_array($flag, $this->flags->all()->toArray())) { |
|
126 | 2 | return true; |
|
127 | } |
||
128 | 2 | return false; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Retrieve the value of an option (eg. --username=jbloggs or --username="jbloggs") |
||
133 | * @param string $name The name of the option to return |
||
134 | * @param type $default An optional default value if its not set. |
||
135 | * @return mixed |
||
136 | */ |
||
137 | 2 | public function getOption($name, $default = false) |
|
138 | { |
||
139 | 2 | return $this->options->get($name, $default); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Returns a command from the command index (eg. new or list). |
||
144 | * @param int $part Will return the Nth command. eg 1 for "./consoleapp new myapp" will return 'new'. |
||
145 | * @param string $default An optional default value if the command index is not set. |
||
146 | */ |
||
147 | 28 | public function getCommand($part, $default = false) |
|
151 | |||
152 | /** |
||
153 | * Detects and sets "flag" type arguments. |
||
154 | * @param string $argument The argument string. |
||
155 | * @return boolean |
||
156 | */ |
||
157 | 54 | private function detectFlags($argument) |
|
165 | |||
166 | /** |
||
167 | * Detects and sets "concatenated flag" type arguments. |
||
168 | * @param string $argument The argument string. |
||
169 | * @return boolean |
||
170 | */ |
||
171 | 54 | private function detectConcatFlag($argument) |
|
181 | |||
182 | /** |
||
183 | * Detects and sets "option" type arguments. |
||
184 | * @param type $argument The argument string. |
||
185 | * @return boolean |
||
186 | */ |
||
187 | 54 | private function detectOptions($argument) |
|
200 | } |
||
201 |