-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathArguments.php
More file actions
492 lines (426 loc) · 11.9 KB
/
Arguments.php
File metadata and controls
492 lines (426 loc) · 11.9 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* PHP Command Line Tools
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.
*
* @author James Logsdon <dwarf@girsbrain.org>
* @copyright 2010 James Logsdom (http://girsbrain.org)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace cli;
use cli\arguments\Argument;
use cli\arguments\HelpScreen;
use cli\arguments\InvalidArguments;
use cli\arguments\Lexer;
/**
* Parses command line arguments.
*/
class Arguments implements \ArrayAccess {
protected $_flags = array();
protected $_options = array();
protected $_strict = false;
protected $_input = array();
protected $_invalid = array();
protected $_parsed;
protected $_lexer;
/**
* Initializes the argument parser. If you wish to change the default behaviour
* you may pass an array of options as the first argument. Valid options are
* `'help'` and `'strict'`, each a boolean.
*
* `'help'` is `true` by default, `'strict'` is false by default.
*
* @param array $options An array of options for this parser.
*/
public function __construct($options = array()) {
$options += array(
'strict' => false,
'input' => array_slice($_SERVER['argv'], 1)
);
$this->_input = $options['input'];
$this->setStrict($options['strict']);
if (isset($options['flags'])) {
$this->addFlags($options['flags']);
}
if (isset($options['options'])) {
$this->addOptions($options['options']);
}
}
/**
* Get the list of arguments found by the defined definitions.
*
* @return array
*/
public function getArguments() {
if (!isset($this->_parsed)) {
$this->parse();
}
return $this->_parsed;
}
public function getHelpScreen() {
return new HelpScreen($this);
}
/**
* Encodes the parsed arguments as JSON.
*
* @return string
*/
public function asJSON() {
return json_encode($this->_parsed);
}
/**
* Returns true if a given argument was parsed.
*
* @param mixed $offset An Argument object or the name of the argument.
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}
return array_key_exists($offset, $this->_parsed);
}
/**
* Get the parsed argument's value.
*
* @param mixed $offset An Argument object or the name of the argument.
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}
if (isset($this->_parsed[$offset])) {
return $this->_parsed[$offset];
}
}
/**
* Sets the value of a parsed argument.
*
* @param mixed $offset An Argument object or the name of the argument.
* @param mixed $value The value to set
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}
$this->_parsed[$offset] = $value;
}
/**
* Unset a parsed argument.
*
* @param mixed $offset An Argument object or the name of the argument.
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}
unset($this->_parsed[$offset]);
}
/**
* Adds a flag (boolean argument) to the argument list.
*
* @param mixed $flag A string representing the flag, or an array of strings.
* @param array $settings An array of settings for this flag.
* @setting string description A description to be shown in --help.
* @setting bool default The default value for this flag.
* @setting bool stackable Whether the flag is repeatable to increase the value.
* @setting array aliases Other ways to trigger this flag.
* @return $this
*/
public function addFlag($flag, $settings = array()) {
if (is_string($settings)) {
$settings = array('description' => $settings);
}
if (is_array($flag)) {
$settings['aliases'] = $flag;
$flag = array_shift($settings['aliases']);
}
if (isset($this->_flags[$flag])) {
$this->_warn('flag already exists: ' . $flag);
return $this;
}
$settings += array(
'default' => false,
'stackable' => false,
'description' => null,
'aliases' => array()
);
$this->_flags[$flag] = $settings;
return $this;
}
/**
* Add multiple flags at once. The input array should be keyed with the
* primary flag character, and the values should be the settings array
* used by {addFlag}.
*
* @param array $flags An array of flags to add
* @return $this
*/
public function addFlags($flags) {
foreach ($flags as $flag => $settings) {
if (is_numeric($flag)) {
$this->_warn('No flag character given');
continue;
}
$this->addFlag($flag, $settings);
}
return $this;
}
/**
* Adds an option (string argument) to the argument list.
*
* @param mixed $option A string representing the option, or an array of strings.
* @param array $settings An array of settings for this option.
* @setting string description A description to be shown in --help.
* @setting bool default The default value for this option.
* @setting array aliases Other ways to trigger this option.
* @return $this
*/
public function addOption($option, $settings = array()) {
if (is_string($settings)) {
$settings = array('description' => $settings);
}
if (is_array($option)) {
$settings['aliases'] = $option;
$option = array_shift($settings['aliases']);
}
if (isset($this->_options[$option])) {
$this->_warn('option already exists: ' . $option);
return $this;
}
$settings += array(
'default' => null,
'description' => null,
'aliases' => array()
);
$this->_options[$option] = $settings;
return $this;
}
/**
* Add multiple options at once. The input array should be keyed with the
* primary option string, and the values should be the settings array
* used by {addOption}.
*
* @param array $options An array of options to add
* @return $this
*/
public function addOptions($options) {
foreach ($options as $option => $settings) {
if (is_numeric($option)) {
$this->_warn('No option string given');
continue;
}
$this->addOption($option, $settings);
}
return $this;
}
/**
* Enable or disable strict mode. If strict mode is active any invalid
* arguments found by the parser will throw `cli\arguments\InvalidArguments`.
*
* Even if strict is disabled, invalid arguments are logged and can be
* retrieved with `cli\Arguments::getInvalidArguments()`.
*
* @param bool $strict True to enable, false to disable.
* @return $this
*/
public function setStrict($strict) {
$this->_strict = (bool)$strict;
return $this;
}
/**
* Get the list of invalid arguments the parser found.
*
* @return array
*/
public function getInvalidArguments() {
return $this->_invalid;
}
/**
* Get a flag by primary matcher or any defined aliases.
*
* @param mixed $flag Either a string representing the flag or an
* cli\arguments\Argument object.
* @return array
*/
public function getFlag($flag) {
if ($flag instanceOf Argument) {
$obj = $flag;
$flag = $flag->value;
}
if (isset($this->_flags[$flag])) {
return $this->_flags[$flag];
}
foreach ($this->_flags as $master => $settings) {
if (in_array($flag, (array)$settings['aliases'])) {
if (isset($obj)) {
$obj->key = $master;
}
$cache[$flag] =& $settings;
return $settings;
}
}
}
public function getFlags() {
return $this->_flags;
}
public function hasFlags() {
return !empty($this->_flags);
}
/**
* Returns true if the given argument is defined as a flag.
*
* @param mixed $argument Either a string representing the flag or an
* cli\arguments\Argument object.
* @return bool
*/
public function isFlag($argument) {
return (null !== $this->getFlag($argument));
}
/**
* Returns true if the given flag is stackable.
*
* @param mixed $flag Either a string representing the flag or an
* cli\arguments\Argument object.
* @return bool
*/
public function isStackable($flag) {
$settings = $this->getFlag($flag);
return isset($settings) && (true === $settings['stackable']);
}
/**
* Get an option by primary matcher or any defined aliases.
*
* @param mixed $option Either a string representing the option or an
* cli\arguments\Argument object.
* @return array
*/
public function getOption($option) {
if ($option instanceOf Argument) {
$obj = $option;
$option = $option->value;
}
if (isset($this->_options[$option])) {
return $this->_options[$option];
}
foreach ($this->_options as $master => $settings) {
if (in_array($option, (array)$settings['aliases'])) {
if (isset($obj)) {
$obj->key = $master;
}
return $settings;
}
}
}
public function getOptions() {
return $this->_options;
}
public function hasOptions() {
return !empty($this->_options);
}
/**
* Returns true if the given argument is defined as an option.
*
* @param mixed $argument Either a string representing the option or an
* cli\arguments\Argument object.
* @return bool
*/
public function isOption($argument) {
return (null != $this->getOption($argument));
}
/**
* Parses the argument list with the given options. The returned argument list
* will use either the first long name given or the first name in the list
* if a long name is not given.
*
* @return array
* @throws arguments\InvalidArguments
*/
public function parse() {
$this->_invalid = array();
$this->_parsed = array();
$this->_lexer = new Lexer($this->_input);
$this->_applyDefaults();
foreach ($this->_lexer as $argument) {
if ($this->_parseFlag($argument)) {
continue;
}
if ($this->_parseOption($argument)) {
continue;
}
array_push($this->_invalid, $argument->raw);
}
if ($this->_strict && !empty($this->_invalid)) {
throw new InvalidArguments($this->_invalid);
}
}
/**
* This applies the default values, if any, of all of the
* flags and options, so that if there is a default value
* it will be available.
*/
private function _applyDefaults() {
foreach($this->_flags as $flag => $settings) {
$this[$flag] = $settings['default'];
}
foreach($this->_options as $option => $settings) {
// If the default is 0 we should still let it be set.
if (!empty($settings['default']) || $settings['default'] === 0) {
$this[$option] = $settings['default'];
}
}
}
private function _warn($message) {
trigger_error('[' . __CLASS__ .'] ' . $message, E_USER_WARNING);
}
private function _parseFlag($argument) {
if (!$this->isFlag($argument)) {
return false;
}
if ($this->isStackable($argument)) {
if (!isset($this[$argument])) {
$this[$argument->key] = 0;
}
$this[$argument->key] += 1;
} else {
$this[$argument->key] = true;
}
return true;
}
private function _parseOption($option) {
if (!$this->isOption($option)) {
return false;
}
// Peak ahead to make sure we get a value.
if ($this->_lexer->end() || !$this->_lexer->peek->isValue) {
$optionSettings = $this->getOption($option->key);
if (empty($optionSettings['default']) && $optionSettings !== 0) {
// Oops! Got no value and no default , throw a warning and continue.
$this->_warn('no value given for ' . $option->raw);
$this[$option->key] = null;
} else {
// No value and we have a default, so we set to the default
$this[$option->key] = $optionSettings['default'];
}
return true;
}
// Store as array and join to string after looping for values
$values = array();
// Loop until we find a flag in peak-ahead
foreach ($this->_lexer as $value) {
array_push($values, $value->raw);
if (!$this->_lexer->end() && !$this->_lexer->peek->isValue) {
break;
}
}
$this[$option->key] = join(' ', $values);
return true;
}
}