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
|
928 |
|
public function __construct() |
45
|
|
|
{ |
46
|
928 |
|
$this->filter = new Filter(); |
47
|
928 |
|
$this->summary = new Summary(); |
48
|
928 |
|
$this->parser = new Parser(); |
49
|
928 |
|
} |
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
|
24 |
|
public function add($argument, array $options = []) |
59
|
|
|
{ |
60
|
24 |
|
if (is_array($argument)) { |
61
|
20 |
|
$this->addMany($argument); |
62
|
20 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
if (is_string($argument)) { |
66
|
20 |
|
$argument = Argument::createFromArray($argument, $options); |
67
|
20 |
|
} |
68
|
|
|
|
69
|
24 |
|
if (!($argument instanceof Argument)) { |
70
|
4 |
|
throw new \Exception('Please provide an argument name or object.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
20 |
|
$this->arguments[$argument->name()] = $argument; |
74
|
20 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add multiple arguments to a CLImate script. |
78
|
|
|
* |
79
|
|
|
* @param array $arguments |
80
|
|
|
*/ |
81
|
20 |
|
protected function addMany(array $arguments = []) |
82
|
|
|
{ |
83
|
20 |
|
foreach ($arguments as $name => $options) { |
84
|
20 |
|
$this->add($name, $options); |
85
|
20 |
|
} |
86
|
20 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Determine if an argument exists. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
8 |
|
public function exists($name) |
95
|
|
|
{ |
96
|
8 |
|
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
|
4 |
|
public function get($name) |
106
|
|
|
{ |
107
|
4 |
|
return isset($this->arguments[$name]) ? $this->arguments[$name]->value() : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieve all arguments. |
112
|
|
|
* |
113
|
|
|
* @return Argument[] |
114
|
|
|
*/ |
115
|
16 |
|
public function all() |
116
|
|
|
{ |
117
|
16 |
|
return $this->arguments; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Determine if an argument has been defined on the command line. |
122
|
|
|
* |
123
|
|
|
* This can be useful for making sure an argument is present on the command |
124
|
|
|
* line before parse()'ing them into argument objects. |
125
|
|
|
* |
126
|
|
|
* @param string $name |
127
|
|
|
* @param array $argv |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
8 |
|
public function defined($name, array $argv = null) |
132
|
|
|
{ |
133
|
|
|
// The argument isn't defined if it's not defined by the calling code. |
134
|
8 |
|
if (!$this->exists($name)) { |
135
|
8 |
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
8 |
|
$argument = $this->arguments[$name]; |
139
|
8 |
|
$command_arguments = $this->parser->arguments($argv); |
140
|
|
|
|
141
|
8 |
|
foreach ($command_arguments as $command_argument) { |
142
|
8 |
|
if ($this->isArgument($argument, $command_argument)) { |
143
|
8 |
|
return true; |
144
|
|
|
} |
145
|
8 |
|
} |
146
|
|
|
|
147
|
8 |
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Check if the defined argument matches the command argument. |
152
|
|
|
* |
153
|
|
|
* @param Argument $argument |
154
|
|
|
* @param string $command_argument |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
8 |
|
protected function isArgument($argument, $command_argument) |
159
|
|
|
{ |
160
|
|
|
$possibilities = [ |
161
|
8 |
|
$argument->prefix() => "-{$argument->prefix()}", |
162
|
8 |
|
$argument->longPrefix() => "--{$argument->longPrefix()}", |
163
|
8 |
|
]; |
164
|
|
|
|
165
|
8 |
|
foreach ($possibilities as $key => $search) { |
166
|
8 |
|
if ($key && strpos($command_argument, $search) === 0) { |
167
|
8 |
|
return true; |
168
|
|
|
} |
169
|
8 |
|
} |
170
|
|
|
|
171
|
8 |
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Retrieve all arguments as key/value pairs. |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
4 |
|
public function toArray() |
180
|
|
|
{ |
181
|
4 |
|
$return = []; |
182
|
|
|
|
183
|
4 |
|
foreach ($this->all() as $name => $argument) { |
184
|
4 |
|
$return[$name] = $argument->value(); |
185
|
4 |
|
} |
186
|
|
|
|
187
|
4 |
|
return $return; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set a program's description. |
192
|
|
|
* |
193
|
|
|
* @param string $description |
194
|
|
|
*/ |
195
|
4 |
|
public function description($description) |
196
|
|
|
{ |
197
|
4 |
|
$this->description = trim($description); |
198
|
4 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Output a script's usage statement. |
202
|
|
|
* |
203
|
|
|
* @param CLImate $climate |
204
|
|
|
* @param array $argv |
205
|
|
|
*/ |
206
|
4 |
|
public function usage(CLImate $climate, array $argv = null) |
207
|
|
|
{ |
208
|
4 |
|
$this->summary |
209
|
4 |
|
->setClimate($climate) |
210
|
4 |
|
->setDescription($this->description) |
211
|
4 |
|
->setCommand($this->parser->command($argv)) |
212
|
4 |
|
->setFilter($this->filter, $this->all()) |
213
|
4 |
|
->output(); |
214
|
4 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Parse command line arguments into CLImate arguments. |
218
|
|
|
* |
219
|
|
|
* @throws \Exception if required arguments aren't defined. |
220
|
|
|
* @param array $argv |
221
|
|
|
*/ |
222
|
12 |
|
public function parse(array $argv = null) |
223
|
|
|
{ |
224
|
12 |
|
$this->parser->setFilter($this->filter, $this->all()); |
225
|
|
|
|
226
|
12 |
|
$this->parser->parse($argv); |
227
|
8 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get the trailing arguments |
231
|
|
|
* |
232
|
|
|
* @return string|null |
233
|
|
|
*/ |
234
|
4 |
|
public function trailing() |
235
|
|
|
{ |
236
|
4 |
|
return $this->parser->trailing(); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|