1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Manager.php |
4
|
|
|
* |
5
|
|
|
* PHP version 7.3 and up. |
6
|
|
|
* |
7
|
|
|
* @category Core |
8
|
|
|
* @package Redbox_Cli |
9
|
|
|
* @author Johnny Mast <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
11
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
12
|
|
|
* @since 1.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Redbox\Cli\Arguments; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The manager class is the main interface for interacting |
19
|
|
|
* with the arguments part of Redbox-cli. |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Redbox_Cli |
23
|
|
|
* @author Johnny Mast <[email protected]> |
24
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
25
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
|
|
class Manager |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* An array of arguments passed to the program. |
32
|
|
|
* |
33
|
|
|
* @var array $arguments |
34
|
|
|
*/ |
35
|
|
|
protected $arguments = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* An array containing the parsed values. |
39
|
|
|
* |
40
|
|
|
* @var array $values ; |
41
|
|
|
*/ |
42
|
|
|
protected $values = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* An array that contains all the default values |
46
|
|
|
* that are passed to the manager. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $defaultvalues = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* An instance of the argument options parser. |
54
|
|
|
* |
55
|
|
|
* @var \Redbox\Cli\Arguments\Parser |
56
|
|
|
*/ |
57
|
|
|
protected $parser; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* An instance of the argument options filter. |
61
|
|
|
* |
62
|
|
|
* @var \Redbox\Cli\Arguments\Filter |
63
|
|
|
*/ |
64
|
|
|
protected $filter; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Manager constructor. |
68
|
|
|
*/ |
69
|
13 |
|
public function __construct() |
70
|
|
|
{ |
71
|
13 |
|
$this->parser = new Parser($this); |
72
|
13 |
|
$this->filter = new Filter; |
73
|
13 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Prints out the usage message to the user. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
2 |
|
public function usage(): void |
81
|
|
|
{ |
82
|
2 |
|
$requiredArguments = $this->filter->required(); |
83
|
2 |
|
$optionalArguments = $this->filter->optional(); |
84
|
2 |
|
$allArguments = array_merge($requiredArguments, $optionalArguments); |
85
|
2 |
|
$command = $this->parser->getCommand(); |
86
|
2 |
|
$args = array(); |
87
|
|
|
|
88
|
2 |
|
$num_required = count($requiredArguments); |
89
|
2 |
|
$num_optional = count($optionalArguments); |
90
|
|
|
|
91
|
2 |
|
echo "Usage: " . $command . " "; |
92
|
|
|
|
93
|
2 |
|
foreach ($allArguments as $argument) { |
94
|
|
|
/** |
95
|
|
|
* Type of Argument. |
96
|
|
|
* |
97
|
|
|
* @var Argument $argument |
98
|
|
|
*/ |
99
|
2 |
|
$args[] = '[' . $argument->usageInfo() . ']'; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$args = implode(' ', $args); |
103
|
2 |
|
echo $args . "\n\n"; |
104
|
|
|
|
105
|
2 |
|
if ($num_required) { |
106
|
2 |
|
echo "Required Arguments:\n"; |
107
|
2 |
|
foreach ($requiredArguments as $argument) { |
108
|
2 |
|
echo $argument->usageLine(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
if ($num_required && $num_optional) { |
113
|
1 |
|
echo "\n"; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
if ($num_optional) { |
117
|
1 |
|
echo "Optional Arguments:\n"; |
118
|
1 |
|
foreach ($optionalArguments as $argument) { |
119
|
1 |
|
echo $argument->usageLine(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
echo "\n"; |
124
|
2 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Determine if a given argument has a default value or not. |
128
|
|
|
* One thing to note is that if having no info about the argument |
129
|
|
|
* (being a key in xx is not set) we will return false as well. |
130
|
|
|
* |
131
|
|
|
* @param string $argument The argument to check. |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
5 |
|
public function hasDefaultValue(string $argument): bool |
136
|
|
|
{ |
137
|
5 |
|
if (isset($this->defaultvalues[$argument]) === true) { |
138
|
3 |
|
return true; |
139
|
|
|
} |
140
|
2 |
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set if a argument has defaulted to the default argument or not. |
145
|
|
|
* |
146
|
|
|
* @param string $argument The argument name. |
147
|
|
|
* @param bool $default The value to assign. |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
5 |
|
public function setHasDefaultValue(string $argument = "", bool $default = false) |
152
|
|
|
{ |
153
|
5 |
|
$this->defaultvalues[$argument] = $default; |
154
|
5 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the default value for a argument. |
158
|
|
|
* |
159
|
|
|
* @param string $argument The argument key |
160
|
|
|
* |
161
|
|
|
* @return mixed |
162
|
|
|
*/ |
163
|
2 |
|
public function getDefaultValue(string $argument) |
164
|
|
|
{ |
165
|
2 |
|
if ($this->hasDefaultValue($argument)) { |
166
|
1 |
|
return $this->get($argument); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Check to see if a argument is used. |
174
|
|
|
* |
175
|
|
|
* @param string $argument The name of the argument. |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
2 |
|
public function has(string $argument): bool |
180
|
|
|
{ |
181
|
2 |
|
return (isset($this->values[$argument])); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set a parsed argument. |
186
|
|
|
* |
187
|
|
|
* @param string $argument The argument name. |
188
|
|
|
* @param mixed $value The argument value. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
10 |
|
public function set(string $argument, $value): void |
193
|
|
|
{ |
194
|
10 |
|
$this->values[$argument] = $value; |
195
|
10 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return any set argument or false if the argument is unknown. |
199
|
|
|
* |
200
|
|
|
* @param string $argument The name of the argument. |
201
|
|
|
* |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
5 |
|
public function get(string $argument) |
205
|
|
|
{ |
206
|
5 |
|
if (isset($this->values[$argument]) === false) { |
207
|
1 |
|
return false; |
208
|
|
|
} |
209
|
4 |
|
return $this->values[$argument]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return all given arguments. |
214
|
|
|
* |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
9 |
|
public function all(): array |
218
|
|
|
{ |
219
|
9 |
|
return $this->arguments; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* This function will be called if we can add an array of commandline arguments |
224
|
|
|
* to parse. |
225
|
|
|
* |
226
|
|
|
* @param array $arguments The argument options. |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
* @throws \Exception |
230
|
|
|
*/ |
231
|
9 |
|
protected function addMany(array $arguments = []): void |
232
|
|
|
{ |
233
|
9 |
|
foreach ($arguments as $name => $options) { |
234
|
9 |
|
$this->add($name, $options); |
235
|
|
|
} |
236
|
9 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Add arguments to the list, this could be one or an array of arguments. |
240
|
|
|
* |
241
|
|
|
* @param mixed $argument The argument name or array arguments. |
242
|
|
|
* @param array $options The argument options. |
243
|
|
|
* |
244
|
|
|
* @return void |
245
|
|
|
* @throws \Exception |
246
|
|
|
*/ |
247
|
9 |
|
public function add($argument, $options = []): void |
248
|
|
|
{ |
249
|
9 |
|
if (is_array($argument) === true) { |
250
|
9 |
|
$this->addMany($argument); |
251
|
9 |
|
return; |
252
|
|
|
} |
253
|
|
|
|
254
|
9 |
|
$options['name'] = $argument; |
255
|
9 |
|
$arg = new Argument($options); |
256
|
|
|
|
257
|
9 |
|
$this->arguments[$argument] = $arg; |
258
|
9 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Go ahead and parse the arguments given. |
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
* @throws \Exception |
265
|
|
|
*/ |
266
|
9 |
|
public function parse(): void |
267
|
|
|
{ |
268
|
9 |
|
$this->parser->setFilter($this->filter, $this->all()); |
269
|
9 |
|
$this->parser->parse(); |
270
|
8 |
|
} |
271
|
|
|
} |
272
|
|
|
|