-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathArguments.php
More file actions
264 lines (235 loc) · 7.07 KB
/
Copy pathArguments.php
File metadata and controls
264 lines (235 loc) · 7.07 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
<?php
namespace GetOpt;
use GetOpt\ArgumentException\Unexpected;
/**
* Class Arguments
*
* @package GetOpt
* @author Thomas Flori <thflori@gmail.com>
*/
class Arguments
{
/** @var string[] */
protected $arguments;
/**
* Create an Arguments object
*
* @param array $arguments
*/
public function __construct(array $arguments)
{
$this->arguments = $arguments;
}
/**
* Process the arguments for $getopt
*
* Stores operands using $addOperand callback.
*
* @param GetOpt $getopt
* @param callable $setOption
* @param callable $setCommand
* @param callable $addOperand
* @return bool
*/
public function process(GetOpt $getopt, callable $setOption, callable $setCommand, callable $addOperand): bool
{
$operands = [];
while (($arg = array_shift($this->arguments)) !== null) {
if ($this->isMeta($arg)) {
// everything from here are operands
$this->flushOperands($operands, $addOperand);
$this->flushOperands($this->arguments, $addOperand);
return true;
}
if ($this->isValue($arg)) {
$operands[] = $arg;
if (count($getopt->getOperands()) === 0 && $command = $getopt->getCommand(implode(' ', $operands))) {
$setCommand($command);
$operands = [];
}
} else {
$this->flushOperands($operands, $addOperand);
}
if ($this->isLongOption($arg)) {
$setOption($this->longName($arg), function (Option $option = null) use ($arg) {
return $this->value($arg, null, $option);
});
continue;
}
// the only left is short options
foreach ($this->shortNames($arg) as $name) {
$requestedValue = false;
$setOption($name, function (Option $option = null) use ($arg, $name, &$requestedValue) {
$requestedValue = true;
return $this->value($arg, $name, $option);
});
if ($requestedValue) {
// when there is a value it was the last option
break;
}
}
}
$this->flushOperands($operands, $addOperand);
return true;
}
/**
* Add operands and reset the array
*
* @param array $operands
* @param callable $addOperand
*/
protected function flushOperands(array &$operands, callable $addOperand)
{
foreach ($operands as $operand) {
$addOperand($operand);
}
$operands = [];
}
/**
* Check if $arg is an option
*
* @param string $arg
* @return bool
*/
protected function isOption($arg): bool
{
return !$this->isValue($arg) && !$this->isMeta($arg);
}
/**
* Check if $arg is a value
*
* @param string $arg
* @return bool
*/
protected function isValue($arg): bool
{
return (empty($arg) || $arg === '-' || 0 !== strpos($arg, '-'));
}
/**
* Check if $arg is meta '--'
*
* @param string $arg
* @return bool
*/
protected function isMeta($arg): bool
{
return $arg && $arg === '--';
}
/**
* Check if $arg is a long option
*
* @param $arg
* @return bool
*/
protected function isLongOption($arg): bool
{
return $this->isOption($arg) && $arg[1] === '-';
}
/**
* Get the long option name from $arg
*
* @param string $arg
* @return string
*/
protected function longName(string $arg): string
{
$name = substr($arg, 2);
$p = strpos($name, '=');
return $p ? substr($name, 0, $p) : $name;
}
/**
* Get all short option names from $arg
*
* @param string $arg
* @return string[] (single character string multi byte safe)
*/
protected function shortNames(string $arg): array
{
if (!$this->isOption($arg) || $this->isLongOption($arg)) {
return [];
}
return array_map(function ($i) use ($arg) {
return mb_substr($arg, $i, 1);
}, range(1, mb_strlen($arg) -1));
}
/**
* Get the value for an option
*
* $name might be the short name to separate the value from the argument in `-abcppassword`.
*
* Returns the value inside $arg or the next argument when it is a value.
*
* @param string $arg
* @param string $name
* @param ?Option $option
* @return ?string
*/
protected function value(string $arg, $name = null, Option $option = null): ?string
{
$p = strpos($arg, $this->isLongOption($arg) ? '=' : $name);
if ($this->isLongOption($arg) && $p || !$this->isLongOption($arg) && $p < strlen($arg)-1) {
return substr($arg, $p+1);
}
if (!empty($this->arguments) && (
$option && in_array($option->getMode(), [GetOpt::REQUIRED_ARGUMENT, GetOpt::MULTIPLE_ARGUMENT]) ||
$this->isValue($this->arguments[0])
)) {
return array_shift($this->arguments);
}
return null;
}
/**
* Parse arguments from argument string
*
* @param string $argsString
* @return Arguments
*/
public static function fromString(string $argsString): Arguments
{
$argv = [ '' ];
$argsString = trim($argsString);
$argc = 0;
if (empty($argsString)) {
return new self([]);
}
$state = 'n'; // states: n (normal), d (double quoted), s (single quoted)
for ($i = 0; $i < strlen($argsString); $i++) {
$char = $argsString[$i];
switch ($state) {
case 'n':
if ($char === '\'') {
$state = 's';
} elseif ($char === '"') {
$state = 'd';
} elseif (in_array($char, [ "\n", "\t", ' ' ])) {
$argc++;
$argv[$argc] = '';
} else {
$argv[$argc] .= $char;
}
break;
case 's':
if ($char === '\'') {
$state = 'n';
} elseif ($char === '\\') {
$i++;
$argv[$argc] .= $argsString[$i];
} else {
$argv[$argc] .= $char;
}
break;
case 'd':
if ($char === '"') {
$state = 'n';
} elseif ($char === '\\') {
$i++;
$argv[$argc] .= $argsString[$i];
} else {
$argv[$argc] .= $char;
}
break;
}
}
return new self($argv);
}
}