-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathGetOpt.php
More file actions
548 lines (480 loc) · 14.4 KB
/
Copy pathGetOpt.php
File metadata and controls
548 lines (480 loc) · 14.4 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
namespace GetOpt;
use GetOpt\ArgumentException\Missing;
use GetOpt\ArgumentException\Unexpected;
/**
* Class GetOpt
*
* @package GetOpt
* @author Thomas Flori <thflori@gmail.com>
*/
class GetOpt implements \Countable, \ArrayAccess, \IteratorAggregate
{
const NO_ARGUMENT = ':noArg';
const REQUIRED_ARGUMENT = ':requiredArg';
const OPTIONAL_ARGUMENT = ':optionalArg';
const MULTIPLE_ARGUMENT = ':multipleArg';
const SETTING_SCRIPT_NAME = 'scriptName';
const SETTING_DEFAULT_MODE = 'defaultMode';
const SETTING_STRICT_OPTIONS = 'strictOptions';
const SETTING_STRICT_OPERANDS = 'strictOperands';
use WithOptions {
getOption as getOptionObject;
getOptions as getOptionObjects;
}
use WithOperands {
getOperand as getOperandObject;
getOperands as getOperandObjects;
}
use WithMagicGetter;
/** @var HelpInterface */
protected $help;
/** @var array */
protected $settings = [
self::SETTING_STRICT_OPTIONS => true,
self::SETTING_STRICT_OPERANDS => false,
];
/** @var int */
protected $operandsCount = 0;
/** @var CommandInterface[] */
protected $commands = [];
/** The command that is executed determined by process
* @var CommandInterface */
protected $command;
/** @var string[] */
protected $additionalOperands = [];
/** @var array */
protected $additionalOptions = [];
/** @var Translator */
protected static $translator;
/**
* Creates a new GetOpt object.
*
* The argument $options can be either a string in the format accepted by the PHP library
* function getopt() or an array.
*
* @param array|string $options
* @param array $settings
* @link https://www.gnu.org/s/hello/manual/libc/Getopt.html GNU GetOpt manual
*/
public function __construct($options = null, array $settings = [])
{
$this->set(
self::SETTING_SCRIPT_NAME,
isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (
isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : null
)
);
foreach ($settings as $setting => $value) {
$this->set($setting, $value);
}
if ($options !== null) {
$this->addOptions($options);
}
}
/**
* Set $setting to $value
*
* @param string $setting
* @param mixed $value
* @return self
*/
public function set(string $setting, $value): GetOpt
{
switch ($setting) {
case self::SETTING_DEFAULT_MODE:
OptionParser::$defaultMode = $value;
break;
default:
$this->settings[$setting] = $value;
break;
}
return $this;
}
/**
* Get the current value of $setting
*
* @param string $setting
* @return mixed
*/
public function get(string $setting)
{
return isset($this->settings[$setting]) ? $this->settings[$setting] : null;
}
/**
* Process the given $arguments
*
* Sets the value for defined options, operands and the command.
*
* @param array|string|Arguments $arguments
*/
public function process($arguments = null)
{
if ($arguments === null) {
$arguments = isset($_SERVER['argv']) ? array_slice($_SERVER['argv'], 1) : [];
$arguments = new Arguments($arguments);
} elseif (is_array($arguments)) {
$arguments = new Arguments($arguments);
} elseif (is_string($arguments)) {
$arguments = Arguments::fromString($arguments);
} elseif (!$arguments instanceof Arguments) {
throw new \InvalidArgumentException(
'$arguments has to be an instance of Arguments, an arguments string, an array of arguments or null'
);
}
$setOption = function ($name, callable $getValue) {
$option = $this->getOptionObject($name);
if (!$option) {
if (!$this->get(self::SETTING_STRICT_OPTIONS)) {
$value = $getValue() ?: 1;
if (isset($this->additionalOptions[$name]) &&
is_int($value) && is_int($this->additionalOptions[$name])
) {
$value += $this->additionalOptions[$name];
}
$this->additionalOptions[$name] = $value;
return;
} else {
throw new Unexpected(sprintf(self::translate('option-unknown'), $name));
}
}
$option->setValue($option->getMode() !== GetOpt::NO_ARGUMENT ? $getValue($option) : null);
};
$setCommand = function (CommandInterface $command) {
$this->addOptions($command->getOptions());
$this->addOperands($command->getOperands());
$this->command = $command;
};
$addOperand = function ($value) {
$operand = $this->nextOperand();
if ($operand) {
$operand->setValue($value);
} elseif ($this->get(self::SETTING_STRICT_OPERANDS)) {
throw new Unexpected(sprintf(
self::translate('no-more-operands'),
$value
));
} else {
$this->additionalOperands[] = $value;
}
};
$this->additionalOptions = [];
$this->additionalOperands = [];
$this->operandsCount = 0;
$arguments->process($this, $setOption, $setCommand, $addOperand);
if (($operand = $this->nextOperand()) && $operand->isRequired() &&
(!$operand->isMultiple() || count($operand->getValue()) === 0)
) {
throw new Missing(sprintf(self::translate('operand-missing'), $operand->getName()));
}
}
/**
* Get an option by $name
*
* @param string $name Short or long name of the option
* @param bool $object Get the definition object instead of the current value.
* @return Option|mixed
*/
public function getOption(string $name, $object = false)
{
$option = $this->getOptionObject($name);
if ($object) {
return $option;
}
if ($option) {
return $option->getValue();
}
return isset($this->additionalOptions[$name]) ? $this->additionalOptions[$name] : null;
}
/**
* Returns the list of options with a value.
*
* @return array
*/
public function getOptions(): array
{
$result = [];
foreach ($this->options as $option) {
$value = $option->getValue();
if ($value !== null) {
$result[$option->getShort() ?: $option->getLong()] = $value;
if ($short = $option->getShort()) {
$result[$short] = $value;
}
if ($long = $option->getLong()) {
$result[$long] = $value;
}
}
}
return $result + $this->additionalOptions;
}
/**
* Add an array of $commands
*
* @param Command[] $commands
* @return self
*/
public function addCommands(array $commands): GetOpt
{
foreach ($commands as $command) {
$this->addCommand($command);
}
return $this;
}
/**
* Add a $command
*
* @param CommandInterface $command
* @return self
*/
public function addCommand(CommandInterface $command): GetOpt
{
foreach ($command->getOptions() as $option) {
if ($this->conflicts($option)) {
throw new \InvalidArgumentException('$command has conflicting options');
}
}
$this->commands[$command->getName()] = $command;
return $this;
}
/**
* Get the current or a named command.
*
* @param string $name
* @return CommandInterface|null
*/
public function getCommand($name = null): ?CommandInterface
{
if ($name !== null) {
return isset($this->commands[$name]) ? $this->commands[$name] : null;
}
return $this->command;
}
/**
* @return CommandInterface[]
*/
public function getCommands(): array
{
return $this->commands;
}
/**
* Check if commands are defined
*
* @return bool
*/
public function hasCommands(): bool
{
return !empty($this->commands);
}
/**
* Get the next operand
*
* @return Operand|null
*/
protected function nextOperand(): ?Operand
{
if (isset($this->operands[$this->operandsCount])) {
$operand = $this->operands[$this->operandsCount];
if (!$operand->isMultiple()) {
$this->operandsCount++;
}
return $operand;
}
return null;
}
/**
* Returns the list of operands. Must be invoked after parse().
*
* @return Operand[]
*/
public function getOperands(): array
{
$operandValues = [];
foreach ($this->getOperandObjects() as $operand) {
$value = $operand->getValue();
if ($value === null) {
continue;
}
if ($operand->isMultiple()) {
$operandValues = array_merge($operandValues, $value);
} else {
$operandValues[] = $value;
}
}
return array_merge($operandValues, $this->additionalOperands);
}
/**
* Returns the nth operand (starting with 0), or null if it does not exist.
*
* When $index is a string it returns the current value or the default value for the named operand.
*
* @param int|string $index
* @return mixed
*/
public function getOperand($index)
{
$operand = $this->getOperandObject($index);
if ($operand) {
return $operand->getValue();
} elseif (is_int($index)) {
$i = $index - count($this->operands);
return $i >= 0 && isset($this->additionalOperands[$i]) ? $this->additionalOperands[$i] : null;
}
return null;
}
/**
* Define a custom Help object
*
* @param HelpInterface $help
* @return self
* @codeCoverageIgnore trivial
*/
public function setHelp(HelpInterface $help): GetOpt
{
$this->help = $help;
return $this;
}
/**
* Translate $key
*
* Returns the translation for the given key; falls back to English if it is
* not localized in the configured language, and ultimately returns the key
* itself should it not exist in the English language file.
*
* @param string $key
* @return string
*/
public static function translate(string $key): string
{
return self::getTranslator()->translate($key);
}
/**
* Get the translator instance
*
* @return Translator
*/
protected static function getTranslator(): Translator
{
if (self::$translator === null) {
self::$translator = new Translator;
}
return self::$translator;
}
/**
* Set language to $language
*
* The language can either be a known language from resources/localization (feel free to contribute your language)
* or a path to a file that returns an array like the files in resources/localization.
*
* @param string $language
* @return bool Whether the language change was successful
*/
public static function setLang(string $language): bool
{
return self::getTranslator()->setLanguage($language);
}
/**
* Get the current Help instance
*
* @return HelpInterface
*/
public function getHelp(): HelpInterface
{
if (!$this->help) {
$this->help = new Help();
}
return $this->help;
}
/**
* Returns an usage information text generated from the given options.
*
* The $padding got removed due to refactoring. Help is an own class now. You can change the layout by using a
* custom template or using a custom help formatter (has to implement HelpInterface)
*
* @see Help for setting a custom template
* @see HelpInterface for creating an custom help formatter
* @param array $data This data will be forwarded to HelpInterface::render and is available in templates
* @return string
*/
public function getHelpText(array $data = []): string
{
return $this->getHelp()->render($this, $data);
}
// array functions
/**
* @inheritDoc
*
* @return \Traversable
* @throws \Exception
*/
public function getIterator(): \Traversable
{
$result = [];
foreach ($this->options as $option) {
if (($value = $option->getValue()) !== null) {
$name = $option->getLong() ?: $option->getShort();
$result[$name] = $value;
}
}
return new \ArrayIterator($result + $this->additionalOptions);
}
/**
* @inheritDoc
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
{
$option = $this->getOptionObject($offset);
if ($option && $option->getValue() !== null) {
return true;
}
return isset($this->additionalOptions[$offset]);
}
/**
* @inheritDoc
*
* @param mixed $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
$option = $this->getOptionObject($offset);
if ($option) {
return $option->getValue();
}
return isset($this->additionalOptions[$offset]) ? $this->additionalOptions[$offset] : null;
}
/**
* @inheritDoc
*
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
throw new \LogicException('Read only array access');
}
/**
* @inheritDoc
*
* @param mixed $offset
* @throws \LogicException
*/
public function offsetUnset($offset): void
{
throw new \LogicException('Read only array access');
}
/**
* @inheritDoc
*
* @return int
* @throws \Exception
*/
public function count(): int
{
return $this->getIterator()->count();
}
}