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