-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathManager.php
More file actions
281 lines (245 loc) · 6.45 KB
/
Manager.php
File metadata and controls
281 lines (245 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
namespace League\CLImate\Argument;
use League\CLImate\CLImate;
use League\CLImate\Exceptions\InvalidArgumentException;
class Manager
{
/**
* An array of arguments passed to the program.
*
* @var Argument[] $arguments
*/
protected $arguments = [];
/**
* A program's description.
*
* @var string $description
*/
protected $description;
/**
* Filter class to find various types of arguments
*
* @var \League\CLImate\Argument\Filter $filter
*/
protected $filter;
/**
* Summary builder class
*
* @var \League\CLImate\Argument\Summary $summary
*/
protected $summary;
/**
* Argument parser class
*
* @var \League\CLImate\Argument\Parser $parser
*/
protected $parser;
public function __construct()
{
$this->filter = new Filter();
$this->summary = new Summary();
$this->parser = new Parser();
}
/**
* Add an argument.
*
* @param Argument|string|array $argument
* @param $options
*
* @return void
* @throws InvalidArgumentException if $argument isn't an array or Argument object.
*/
public function add($argument, array $options = [])
{
if (is_array($argument)) {
$this->addMany($argument);
return;
}
if (is_string($argument)) {
$argument = Argument::createFromArray($argument, $options);
}
if (!$argument instanceof Argument) {
throw new InvalidArgumentException('Please provide an argument name or object.');
}
$this->arguments[$argument->name()] = $argument;
}
/**
* Add multiple arguments to a CLImate script.
*
* @param array $arguments
*/
protected function addMany(array $arguments = [])
{
foreach ($arguments as $name => $options) {
$this->add($name, $options);
}
}
/**
* Determine if an argument exists.
*
* @param string $name
* @return bool
*/
public function exists($name)
{
return isset($this->arguments[$name]);
}
/**
* Retrieve an argument's value.
*
* @param string $name
* @return string|int|float|bool|null
*/
public function get($name)
{
return isset($this->arguments[$name]) ? $this->arguments[$name]->value() : null;
}
/**
* Retrieve an argument's all values as an array.
*
* @param string $name
* @return string[]|int[]|float[]|bool[]
*/
public function getArray($name)
{
return isset($this->arguments[$name]) ? $this->arguments[$name]->values() : [];
}
/**
* Retrieve all arguments.
*
* @return Argument[]
*/
public function all()
{
return $this->arguments;
}
/**
* Determine if an argument has been defined on the command line.
*
* This can be useful for making sure an argument is present on the command
* line before parse()'ing them into argument objects.
*
* @param string $name
* @param array $argv
*
* @return bool
*/
public function defined($name, ?array $argv = null)
{
// The argument isn't defined if it's not defined by the calling code.
if (!$this->exists($name)) {
return false;
}
$argument = $this->arguments[$name];
$command_arguments = $this->parser->arguments($argv);
foreach ($command_arguments as $command_argument) {
if ($this->isArgument($argument, $command_argument)) {
return true;
}
}
return false;
}
/**
* Check if the defined argument matches the command argument.
*
* @param Argument $argument
* @param string $command_argument
*
* @return bool
*/
protected function isArgument($argument, $command_argument)
{
$prefix = $argument->prefix();
$longPrefix = $argument->longPrefix();
if ($prefix !== null && strpos($command_argument, "-{$prefix}") === 0) {
return true;
}
if ($longPrefix !== null && strpos($command_argument, "--{$longPrefix}") === 0) {
return true;
}
return false;
}
/**
* Retrieve all arguments as key/value pairs.
*
* @return array
*/
public function toArray()
{
$return = [];
foreach ($this->all() as $name => $argument) {
$return[$name] = $argument->value();
}
return $return;
}
/**
* Set a program's description.
*
* @param string $description
*/
public function description($description)
{
$this->description = trim($description);
}
/**
* Output a script's usage statement.
*
* @param CLImate $climate
* @param array $argv
*/
public function usage(CLImate $climate, ?array $argv = null)
{
$this->summary
->setClimate($climate)
->setDescription($this->description)
->setCommand($this->parser->command($argv))
->setFilter($this->filter, $this->all())
->output();
}
/**
* Parse command line arguments into CLImate arguments.
*
* @param array $argv
*/
public function parse(?array $argv = null)
{
$this->parser->setFilter($this->filter, $this->all());
$this->parser->parse($argv);
}
/**
* Get the trailing arguments
*
* @return string|null
*/
public function trailing()
{
return $this->parser->trailing();
}
/**
* Get the trailing arguments as an array
*
* @return array|null
*/
public function trailingArray()
{
return $this->parser->trailingArray();
}
/**
* Returns the list of unknown prefixed arguments and their suggestions.
*
* @return array The list of unknown prefixed arguments and their suggestions.
*/
public function getUnknowPrefixedArgumentsAndSuggestions()
{
return $this->parser->getUnknowPrefixedArgumentsAndSuggestions();
}
/**
* Sets the minimum similarity percentage for finding suggestions.
*
* @param float $percentage The minimum similarity percentage to set.
*/
public function setMinimumSimilarityPercentage(float $percentage)
{
$this->parser->setMinimumSimilarityPercentage($percentage);
}
}