|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* this file is part of magerun |
|
4
|
|
|
* |
|
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace N98\Magento\Application; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Ad-hoc options parser, used to deal with shortcoming of getopt() with Symfony style command-line options, arguments |
|
12
|
|
|
* and |
|
13
|
|
|
*/ |
|
14
|
|
|
final class OptionParser |
|
15
|
|
|
{ |
|
16
|
|
|
private $argv; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(array $argv) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->argv = $argv; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $argv (optional) |
|
|
|
|
|
|
25
|
|
|
* @return OptionParser |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function init(array $argv = null) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
if (null === $argv) { |
|
30
|
|
|
$argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return new self($argv); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Check for long-opt option existance |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $name of long option (e.g. root-dir for --root-dir) |
|
40
|
|
|
* @return null|true |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
public function hasLongOption($name) |
|
43
|
|
|
{ |
|
44
|
|
|
$long = "--$name"; |
|
45
|
|
|
|
|
46
|
|
|
$args = $this->argv; |
|
47
|
|
|
|
|
48
|
|
|
// utility |
|
49
|
|
|
array_shift($args); |
|
50
|
|
|
|
|
51
|
|
|
while ($option = array_shift($args)) { |
|
52
|
|
|
if ('--' === $option) { |
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
$len = strlen($option); |
|
56
|
|
|
if (!$len) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
if ('-' !== $option[0]) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
if ($long === $option) { |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Obtain the non-optional argument of a long-opt |
|
72
|
|
|
* |
|
73
|
|
|
* Note: This method does not differ between a non-existent option |
|
74
|
|
|
* or an option with no argument or an option whichs |
|
75
|
|
|
* argument is missing and then taking the next argument as |
|
76
|
|
|
* the options' argument. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name of long option (e.g. root-dir for --root-dir) |
|
79
|
|
|
* @return null|string option argument if it exists, otherwise null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getLongOptionArgument($name) |
|
82
|
|
|
{ |
|
83
|
|
|
$long = "--$name"; |
|
84
|
|
|
|
|
85
|
|
|
$args = $this->argv; |
|
86
|
|
|
|
|
87
|
|
|
// utility |
|
88
|
|
|
array_shift($args); |
|
89
|
|
|
|
|
90
|
|
|
while ($option = array_shift($args)) { |
|
91
|
|
|
if ('--' === $option) { |
|
92
|
|
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
$len = strlen($option); |
|
95
|
|
|
if (!$len) { |
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
if ('-' !== $option[0]) { |
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
if ($long === $option) { |
|
102
|
|
|
if (null !== $argument = array_shift($args)) { |
|
103
|
|
|
$path = $argument; |
|
104
|
|
|
} |
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
if ($long . '=' === substr($option, 0, $optLen = strlen($long) + 1)) { |
|
108
|
|
|
if ($len > $optLen) { |
|
109
|
|
|
$path = substr($option, $optLen); |
|
110
|
|
|
} |
|
111
|
|
|
break; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return isset($path) ? $path : null; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.