-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParser.php
209 lines (163 loc) · 4.87 KB
/
Parser.php
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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\CommandLine;
/**
* Stupid command line arguments parser.
*/
class Parser
{
public const
Argument = 'argument',
Optional = 'optional',
Repeatable = 'repeatable',
Enum = 'enum',
RealPath = 'realpath',
Normalizer = 'normalizer',
Default = 'default';
#[\Deprecated('use Parser::Argument')]
public const ARGUMENT = self::Argument;
#[\Deprecated('use Parser::Optional')]
public const OPTIONAL = self::Optional;
#[\Deprecated('use Parser::Repeatable')]
public const REPEATABLE = self::Repeatable;
#[\Deprecated('use Parser::Enum')]
public const ENUM = self::Enum;
#[\Deprecated('use Parser::Realpath')]
public const REALPATH = self::RealPath;
#[\Deprecated('use Parser::Default')]
public const VALUE = self::Default;
/** @var array[] */
private array $options = [];
/** @var string[] */
private array $aliases = [];
/** @var string[] */
private array $positional = [];
private string $help;
public function __construct(string $help, array $defaults = [])
{
$this->help = $help;
$this->options = $defaults;
preg_match_all('#^[ \t]+(--?\w.*?)(?: .*\(default: (.*)\)| |\r|$)#m', $help, $lines, PREG_SET_ORDER);
foreach ($lines as $line) {
preg_match_all('#(--?\w[\w-]*)(?:[= ](<.*?>|\[.*?]|\w+)(\.{0,3}))?[ ,|]*#A', $line[1], $m);
if (!count($m[0]) || count($m[0]) > 2 || implode('', $m[0]) !== $line[1]) {
throw new \InvalidArgumentException("Unable to parse '$line[1]'.");
}
$name = end($m[1]);
$opts = $this->options[$name] ?? [];
$this->options[$name] = $opts + [
self::Argument => (bool) end($m[2]),
self::Optional => isset($line[2]) || (substr(end($m[2]), 0, 1) === '[') || isset($opts[self::Default]),
self::Repeatable => (bool) end($m[3]),
self::Enum => count($enums = explode('|', trim(end($m[2]), '<[]>'))) > 1 ? $enums : null,
self::Default => $line[2] ?? null,
];
if ($name !== $m[1][0]) {
$this->aliases[$m[1][0]] = $name;
}
}
foreach ($this->options as $name => $foo) {
if ($name[0] !== '-') {
$this->positional[] = $name;
}
}
}
public function parse(?array $args = null): array
{
if ($args === null) {
$args = isset($_SERVER['argv']) ? array_slice($_SERVER['argv'], 1) : [];
}
$params = [];
reset($this->positional);
$i = 0;
while ($i < count($args)) {
$arg = $args[$i++];
if ($arg[0] !== '-') {
if (!current($this->positional)) {
throw new \Exception("Unexpected parameter $arg.");
}
$name = current($this->positional);
$this->checkArg($this->options[$name], $arg);
if (empty($this->options[$name][self::Repeatable])) {
$params[$name] = $arg;
next($this->positional);
} else {
$params[$name][] = $arg;
}
continue;
}
[$name, $arg] = strpos($arg, '=') ? explode('=', $arg, 2) : [$arg, true];
if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
} elseif (!isset($this->options[$name])) {
throw new \Exception("Unknown option $name.");
}
$opt = $this->options[$name];
if ($arg !== true && empty($opt[self::Argument])) {
throw new \Exception("Option $name has not argument.");
} elseif ($arg === true && !empty($opt[self::Argument])) {
if (isset($args[$i]) && $args[$i][0] !== '-') {
$arg = $args[$i++];
} elseif (empty($opt[self::Optional])) {
throw new \Exception("Option $name requires argument.");
}
}
$this->checkArg($opt, $arg);
if (
!empty($opt[self::Enum])
&& !in_array(is_array($arg) ? reset($arg) : $arg, $opt[self::Enum], true)
&& !(
$opt[self::Optional]
&& $arg === true
)
) {
throw new \Exception("Value of option $name must be " . implode(', or ', $opt[self::Enum]) . '.');
}
if (empty($opt[self::Repeatable])) {
$params[$name] = $arg;
} else {
$params[$name][] = $arg;
}
}
foreach ($this->options as $name => $opt) {
if (isset($params[$name])) {
continue;
} elseif (isset($opt[self::Default])) {
$params[$name] = $opt[self::Default];
} elseif ($name[0] !== '-' && empty($opt[self::Optional])) {
throw new \Exception("Missing required argument <$name>.");
} else {
$params[$name] = null;
}
if (!empty($opt[self::Repeatable])) {
$params[$name] = (array) $params[$name];
}
}
return $params;
}
public function help(): void
{
echo $this->help;
}
public function checkArg(array $opt, &$arg): void
{
if (isset($opt[self::Normalizer])) {
$arg = $opt[self::Normalizer]($arg);
}
if (!empty($opt[self::RealPath])) {
$path = realpath($arg);
if ($path === false) {
throw new \Exception("File path '$arg' not found.");
}
$arg = $path;
}
}
public function isEmpty(): bool
{
return !isset($_SERVER['argv']) || count($_SERVER['argv']) < 2;
}
}