1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2014 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Garden\Cli; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class represents the parsed and validated argument list. |
12
|
|
|
*/ |
13
|
|
|
class Args implements \JsonSerializable, \ArrayAccess { |
14
|
|
|
protected $command; |
15
|
|
|
protected $opts; |
16
|
|
|
protected $args; |
17
|
|
|
protected $meta; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initialize the {@link Args} instance. |
21
|
|
|
* |
22
|
|
|
* @param string $command The name of the command. |
23
|
|
|
* @param array $opts An array of command line options. |
24
|
|
|
* @param array $args A numeric array of command line args. |
25
|
|
|
*/ |
26
|
43 |
|
public function __construct($command = '', $opts = [], $args = []) { |
27
|
43 |
|
$this->command = $command; |
28
|
43 |
|
$this->opts = $opts; |
29
|
43 |
|
$this->args = $args; |
30
|
43 |
|
$this->meta = []; |
31
|
43 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add an argument to the args array. |
35
|
|
|
* |
36
|
|
|
* @param string $value The argument to add. |
37
|
|
|
* @param string|null $index The index to add the arg at. |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
4 |
|
public function addArg($value, $index = null) { |
41
|
4 |
|
if ($index !== null) { |
42
|
3 |
|
$this->args[$index] = $value; |
43
|
|
|
} else { |
44
|
1 |
|
$this->args[] = $value; |
45
|
|
|
} |
46
|
4 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get an argument at a given index. |
51
|
|
|
* |
52
|
|
|
* Arguments can be accessed by name or index. |
53
|
|
|
* |
54
|
|
|
* @param string|int $index |
55
|
|
|
* @param mixed $default The default value to return if the argument is not found. |
56
|
|
|
* @return mixed Returns the argument at {@link $index} or {@link $default}. |
57
|
|
|
*/ |
58
|
3 |
|
public function getArg($index, $default = null) { |
59
|
3 |
|
if (array_key_exists($index, $this->args)) { |
60
|
3 |
|
return $this->args[$index]; |
61
|
2 |
|
} elseif (is_int($index)) { |
62
|
2 |
|
$values = array_values($this->args); |
63
|
2 |
|
if (array_key_exists($index, $values)) { |
64
|
2 |
|
return $values[$index]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
return $default; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set an argument in the args array. |
72
|
|
|
* |
73
|
|
|
* @param string|int $index The index to set at. |
74
|
|
|
* @param mixed $value The value of the arg. |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
1 |
|
public function setArg($index, $value) { |
78
|
1 |
|
$this->args[$index] = $value; |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the args array. |
84
|
|
|
* |
85
|
|
|
* @return array Returns the args array. |
86
|
|
|
*/ |
87
|
35 |
|
public function getArgs() { |
88
|
35 |
|
return $this->args; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the args array. |
93
|
|
|
* |
94
|
|
|
* @param array $args The new args array. |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
35 |
|
public function setArgs(array $args) { |
98
|
35 |
|
$this->args = $args; |
99
|
35 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the name of the command associated with the args. |
104
|
|
|
* |
105
|
|
|
* @return string Returns the name of the command. |
106
|
|
|
*/ |
107
|
40 |
|
public function getCommand() { |
108
|
40 |
|
return $this->command; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the name of the command associated with the args. |
113
|
|
|
* |
114
|
|
|
* @param string $command The new command. |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
4 |
|
public function setCommand($command) { |
118
|
4 |
|
$this->command = $command; |
119
|
4 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get a meta value. |
124
|
|
|
* |
125
|
|
|
* @param string $name The name of the meta value. |
126
|
|
|
* @param mixed $default The default value to return if {@link $name} is not found. |
127
|
|
|
* @return mixed Returns the meta value or {@link $default} if it doesn't exist. |
128
|
|
|
*/ |
129
|
7 |
|
public function getMeta($name, $default = null) { |
130
|
7 |
|
return Cli::val($name, $this->meta, $default); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set a meta value. |
135
|
|
|
* |
136
|
|
|
* @param string $name The name of the meta value. |
137
|
|
|
* @param mixed $value The new meta value. |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
41 |
|
public function setMeta($name, $value) { |
141
|
41 |
|
$this->meta[$name] = $value; |
142
|
41 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the entire options array. |
147
|
|
|
* |
148
|
|
|
* @return array Returns the current options array. |
149
|
|
|
*/ |
150
|
34 |
|
public function getOpts() { |
151
|
34 |
|
return $this->opts; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Sets the entire options array. |
156
|
|
|
* |
157
|
|
|
* @param array $value Pass an array to set a new options array. |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
1 |
|
public function setOpts(array $value) { |
161
|
1 |
|
$this->opts = $value; |
162
|
1 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the value of a passed option. |
167
|
|
|
* |
168
|
|
|
* @param string $option The name of the option to get. |
169
|
|
|
* @param mixed $default The default value if the option does not exist. |
170
|
|
|
* @return mixed Returns the option or {@link $default} if it does not exist. |
171
|
|
|
*/ |
172
|
38 |
|
public function getOpt($option, $default = null) { |
173
|
38 |
|
return Cli::val($option, $this->opts, $default); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set an option. |
178
|
|
|
* |
179
|
|
|
* @param string $option The name of the option. |
180
|
|
|
* @param mixed $value The value of the option. |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
39 |
|
public function setOpt($option, $value) { |
184
|
39 |
|
$this->opts[$option] = $value; |
185
|
39 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return the json serializable data for the args. |
190
|
|
|
* |
191
|
|
|
* @return array Returns an array of data that can be used to serialize the args to json. |
192
|
|
|
*/ |
193
|
1 |
|
public function jsonSerialize() { |
194
|
|
|
return [ |
195
|
1 |
|
'command' => $this->command, |
196
|
1 |
|
'opts' => $this->opts, |
197
|
1 |
|
'args' => $this->args, |
198
|
1 |
|
'meta' => $this->meta |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Whether a offset exists. |
204
|
|
|
* |
205
|
|
|
* @param mixed $offset An offset to check for. |
206
|
|
|
* @return boolean true on success or false on failure. |
207
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
208
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
209
|
|
|
*/ |
210
|
1 |
|
public function offsetExists($offset) { |
211
|
1 |
|
return isset($this->opts[$offset]); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Offset to retrieve. |
216
|
|
|
* |
217
|
|
|
* @param mixed $offset The offset to retrieve. |
218
|
|
|
* @return mixed Can return all value types. |
219
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
220
|
|
|
*/ |
221
|
2 |
|
public function offsetGet($offset) { |
222
|
2 |
|
return $this->getOpt($offset, null); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Offset to set. |
227
|
|
|
* |
228
|
|
|
* @param mixed $offset The offset to assign the value to. |
229
|
|
|
* @param mixed $value The value to set. |
230
|
|
|
* @return void |
231
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
232
|
|
|
*/ |
233
|
1 |
|
public function offsetSet($offset, $value) { |
234
|
1 |
|
$this->setOpt($offset, $value); |
235
|
1 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Offset to unset. |
239
|
|
|
* |
240
|
|
|
* @param mixed $offset The offset to unset. |
241
|
|
|
* @return void |
242
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
243
|
|
|
*/ |
244
|
1 |
|
public function offsetUnset($offset) { |
245
|
1 |
|
unset($this->opts[$offset]); |
246
|
1 |
|
} |
247
|
|
|
} |
248
|
|
|
|