1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\Clip\Utilities; |
4
|
|
|
|
5
|
|
|
use Ballen\Collection\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Clip |
9
|
|
|
* |
10
|
|
|
* A package for speeding up development of PHP console (CLI) applications. |
11
|
|
|
* |
12
|
|
|
* @author Bobby Allen <[email protected]> |
13
|
|
|
* @license https://raw.githubusercontent.com/bobsta63/clip/master/LICENSE |
14
|
|
|
* @link https://github.com/bobsta63/clip |
15
|
|
|
* @link http://www.bobbyallen.me |
16
|
|
|
* |
17
|
|
|
*/ |
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
|
48 |
|
public function __construct(array $arguments = []) |
50
|
|
|
{ |
51
|
|
|
|
52
|
48 |
|
$this->commands = new Collection(); |
53
|
48 |
|
$this->options = new Collection(); |
54
|
48 |
|
$this->flags = new Collection(); |
55
|
48 |
|
$this->arguments = new Collection(); |
56
|
48 |
|
$this->parse($arguments); |
57
|
48 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return the collection of commands (eg. help). |
61
|
|
|
* @return Collection |
62
|
|
|
*/ |
63
|
8 |
|
public function commands() |
64
|
|
|
{ |
65
|
8 |
|
return $this->commands; |
66
|
|
|
} |
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
|
4 |
|
public function arguments() |
91
|
|
|
{ |
92
|
4 |
|
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
|
48 |
|
private function parse($args) |
102
|
|
|
{ |
103
|
48 |
|
foreach ($args as $argument) { |
104
|
46 |
|
$this->arguments->push($argument); |
105
|
46 |
|
if ($this->detectFlags($argument)) { |
106
|
18 |
|
continue; |
107
|
|
|
} |
108
|
46 |
|
if ($this->detectOptions($argument)) { |
109
|
18 |
|
continue; |
110
|
|
|
} |
111
|
46 |
|
if ($this->detectConcatFlag($argument)) { |
112
|
18 |
|
continue; |
113
|
|
|
} |
114
|
46 |
|
$this->commands->push($argument); |
115
|
24 |
|
} |
116
|
48 |
|
} |
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
|
|
|
public function isFlagSet($flag) |
124
|
|
|
{ |
125
|
|
|
if (in_array($flag, $this->flags->all()->toArray())) { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
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
|
|
|
public function getOption($name, $default = false) |
138
|
|
|
{ |
139
|
|
|
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) |
148
|
|
|
{ |
149
|
28 |
|
return $this->commands->get($part, $default); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Detects and sets "flag" type arguments. |
154
|
|
|
* @param string $argument The argument string. |
155
|
|
|
* @return boolean |
156
|
|
|
*/ |
157
|
46 |
|
private function detectFlags($argument) |
158
|
|
|
{ |
159
|
46 |
|
if ((substr($argument, 0, 2) === '--') && (!strpos($argument, '='))) { |
160
|
18 |
|
$this->flags->push(ltrim($argument, '--')); |
161
|
18 |
|
return true; |
162
|
|
|
} |
163
|
46 |
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Detects and sets "concatenated flag" type arguments. |
168
|
|
|
* @param string $argument The argument string. |
169
|
|
|
* @return boolean |
170
|
|
|
*/ |
171
|
46 |
|
private function detectConcatFlag($argument) |
172
|
|
|
{ |
173
|
46 |
|
if (substr($argument, 0, 1) === '-') { |
174
|
18 |
|
for ($i = 1; isset($argument[$i]); $i++) { |
175
|
18 |
|
$this->flags->push($argument[$i]); |
176
|
9 |
|
} |
177
|
18 |
|
return true; |
178
|
|
|
} |
179
|
46 |
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Detects and sets "option" type arguments. |
184
|
|
|
* @param type $argument The argument string. |
185
|
|
|
* @return boolean |
186
|
|
|
*/ |
187
|
46 |
|
private function detectOptions($argument) |
188
|
|
|
{ |
189
|
46 |
|
if (substr($argument, 0, 2) === '--') { |
190
|
18 |
|
$name = substr($argument, 2); |
191
|
18 |
|
$value = ''; |
192
|
18 |
|
if (strpos($name, '=')) { |
193
|
18 |
|
list($name, $value) = explode('=', $argument, 2); |
194
|
9 |
|
} else { |
195
|
|
|
$value = rtrim($argument, $value, ' '); |
196
|
|
|
} |
197
|
18 |
|
$this->options->put(ltrim($name, '-'), trim($value, '"')); |
198
|
18 |
|
return true; |
199
|
|
|
} |
200
|
46 |
|
return false; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|