forked from hoaproject/Console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetOption.php
359 lines (302 loc) · 10.1 KB
/
GetOption.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
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
<?php
/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2017, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace Hoa\Console;
use Hoa\Ustring;
/**
* Class \Hoa\Console\GetOption.
*
* This class is complementary to the \Hoa\Console\Parser class.
* This class manages the options profile for a command, i.e. argument,
* interactivity, option name etc.
* And, of course, it proposes the getOption method, that allow user to loop
* easily the command options/arguments.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class GetOption
{
/**
* Argument: no argument is needed.
*
* @const int
*/
const NO_ARGUMENT = 0;
/**
* Argument: required.
*
* @const int
*/
const REQUIRED_ARGUMENT = 1;
/**
* Argument: optional.
*
* @const int
*/
const OPTIONAL_ARGUMENT = 2;
/**
* Option bucket: name.
*
* @const int
*/
const OPTION_NAME = 0;
/**
* Option bucket: has argument.
*
* @const int
*/
const OPTION_HAS_ARG = 1;
/**
* Option bucket: value.
*
* @const int
*/
const OPTION_VAL = 2;
/**
* Describe the command options (or switches).
* An option is describing like this:
* name, has_arg, val
* (In C, we got the flag data before val, but it does not have sens here).
*
* The name is the option name and the long option value.
* The has_arg is a constant: NO_ARGUMENT, REQUIRED_ARGUMENT, and
* OPTIONAL_ARGUMENT.
* The val is the short option value.
*
* @var array
*/
protected $_options = [];
/**
* Parser.
*
* @var \Hoa\Console\Parser
*/
protected $_parser = null;
/**
* The pipette contains all the short value of options.
*
* @var array
*/
protected $_pipette = [];
/**
* Prepare the pipette.
*
* @param array $options The option definition.
* @param \Hoa\Console\Parser $parser The parser.
* @throws \Hoa\Console\Exception
*/
public function __construct(array $options, Parser $parser)
{
$this->_options = $options;
$this->_parser = $parser;
if (empty($options)) {
$this->_pipette[null] = null;
return;
}
$names = [];
foreach ($options as $i => $option) {
if (isset($option[self::OPTION_NAME])) {
$names[$option[self::OPTION_NAME]] = $i;
}
if (isset($option[self::OPTION_VAL])) {
$names[$option[self::OPTION_VAL]] = $i;
}
}
$_names = array_keys($names);
$switches = $parser->getSwitches();
foreach ($switches as $name => $value) {
if (false === in_array($name, $_names)) {
if (1 === strlen($name)) {
$this->_pipette[] = ['__ambiguous', [
'solutions' => [],
'value' => $value,
'option' => $name
]];
continue;
}
$haystack = implode(';', $_names);
$differences = (int) ceil(strlen($name) / 3);
$searched = Ustring\Search::approximated(
$haystack,
$name,
$differences
);
$solutions = [];
foreach ($searched as $s) {
$h = substr($haystack, $s['i'], $s['l']);
if (false !== strpos($h, ';') ||
false !== in_array($h, array_keys($switches)) ||
false === in_array($h, $_names)) {
continue;
}
$solutions[] = $h;
}
if (empty($solutions)) {
continue;
}
$this->_pipette[] = ['__ambiguous', [
'solutions' => $solutions,
'value' => $value,
'option' => $name
]];
continue;
}
$option = $options[$names[$name]];
$argument = $option[self::OPTION_HAS_ARG];
if (self::NO_ARGUMENT === $argument) {
if (!is_bool($value)) {
$parser->transferSwitchToInput($name, $value);
}
} elseif (self::REQUIRED_ARGUMENT === $argument && !is_string($value)) {
throw new Exception(
'The argument %s requires a value (it is not a switch).',
0,
$name
);
}
$this->_pipette[] = [$option[self::OPTION_VAL], $value];
}
$this->_pipette[null] = null;
reset($this->_pipette);
return;
}
/**
* Get option from the pipette.
*
* @param string &$optionValue Place a variable that will receive the
* value of the current option.
* @param string $short Short options to scan (in a single
* string). If $short = null, all short
* options will be selected.
* @return mixed
*/
public function getOption(&$optionValue, $short = null)
{
static $first = true;
$optionValue = null;
if (true === $this->isPipetteEmpty() && true === $first) {
$first = false;
return false;
}
$k = key($this->_pipette);
$c = current($this->_pipette);
$key = $c[0];
$value = $c[1];
if (null == $k && null === $c) {
reset($this->_pipette);
$first = true;
return false;
}
$allow = [];
if (null === $short) {
foreach ($this->_options as $option) {
$allow[] = $option[self::OPTION_VAL];
}
} else {
$allow = str_split($short);
}
if (!in_array($key, $allow) && '__ambiguous' != $key) {
return false;
}
$optionValue = $value;
$return = $key;
next($this->_pipette);
return $return;
}
/**
* Check if the pipette is empty.
*
* @return bool
*/
public function isPipetteEmpty()
{
return count($this->_pipette) == 1;
}
/**
* Resolve option ambiguity. Please see the special pipette entry
* “ambiguous” in the self::__construct method.
* For a smarter resolving, you could use the console kit (please, see
* Hoa\Console\Dispatcher\Kit).
*
* @param array $solutions Solutions.
* @return void
* @throws \Hoa\Console\Exception
*/
public function resolveOptionAmbiguity(array $solutions)
{
if (!isset($solutions['solutions']) ||
!isset($solutions['value']) ||
!isset($solutions['option'])) {
throw new Exception(
'Cannot resolve option ambiguity because the given solution ' .
'seems to be corruped.',
1
);
}
$choices = $solutions['solutions'];
if (1 > count($choices)) {
throw new Exception(
'Cannot resolve ambiguity, fix your typo in the option %s :-).',
2,
$solutions['option']
);
}
$theSolution = $choices[0];
foreach ($this->_options as $option) {
if ($theSolution == $option[self::OPTION_NAME] ||
$theSolution == $option[self::OPTION_VAL]) {
$argument = $option[self::OPTION_HAS_ARG];
$value = $solutions['value'];
if (self::NO_ARGUMENT === $argument) {
if (!is_bool($value)) {
$this->_parser->transferSwitchToInput($theSolution, $value);
}
} elseif (self::REQUIRED_ARGUMENT === $argument &&
!is_string($value)) {
throw new Exception(
'The argument %s requires a value (it is not a switch).',
3,
$theSolution
);
}
unset($this->_pipette[null]);
$this->_pipette[] = [$option[self::OPTION_VAL], $value];
$this->_pipette[null] = null;
return;
}
}
return;
}
}