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