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