|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Cli; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Ktomk\Pipelines\Cli\Args\Args as ArgsArgs; |
|
9
|
|
|
use Ktomk\Pipelines\Cli\Args\OptionFilterIterator; |
|
10
|
|
|
use Ktomk\Pipelines\Cli\Args\OptionIterator; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Args |
|
14
|
|
|
* |
|
15
|
|
|
* Facade class to access arguments related functionality |
|
16
|
|
|
* |
|
17
|
|
|
* @package Ktomk\Pipelines\Cli |
|
18
|
|
|
*/ |
|
19
|
|
|
class Args extends ArgsArgs |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string $utility name |
|
23
|
|
|
*/ |
|
24
|
|
|
private $utility = ''; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* create from $argv |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $argv |
|
30
|
|
|
* |
|
31
|
|
|
* @throws InvalidArgumentException |
|
32
|
|
|
* |
|
33
|
|
|
* @return Args |
|
34
|
|
|
*/ |
|
35
|
5 |
|
public static function create(array $argv) |
|
36
|
|
|
{ |
|
37
|
5 |
|
if (0 === count($argv)) { |
|
38
|
1 |
|
throw new InvalidArgumentException('There must be at least one argument (the command name)'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
$command = (string)array_shift($argv); |
|
42
|
4 |
|
$args = new self($argv); |
|
43
|
4 |
|
$args->utility = $command; |
|
44
|
|
|
|
|
45
|
4 |
|
return $args; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
21 |
|
public function __construct(array $arguments) |
|
49
|
|
|
{ |
|
50
|
21 |
|
$this->arguments = $arguments; |
|
51
|
21 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* test for option and consume any of them. these options |
|
55
|
|
|
* are all w/o option argument. e.g. check for -v/--verbose. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string|string[] $option |
|
58
|
|
|
* |
|
59
|
|
|
* @throws InvalidArgumentException |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function hasOption($option) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$options = new OptionFilterIterator($this, $option); |
|
66
|
|
|
|
|
67
|
|
|
# consume arguments |
|
68
|
3 |
|
foreach ($options as $index => $argument) { |
|
69
|
3 |
|
unset($this->arguments[$index]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
return isset($index); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* map options on array keyed with options to parameters |
|
77
|
|
|
* |
|
78
|
|
|
* both provided to callback, options as first, parameter as |
|
79
|
|
|
* second parameter. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $map |
|
82
|
|
|
* @param callable $callback |
|
83
|
|
|
* |
|
84
|
|
|
* @return array results |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function mapOption(array $map, $callback) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$results = array(); |
|
89
|
1 |
|
foreach ($map as $option => $parameter) { |
|
90
|
1 |
|
$result = array(false, null); |
|
91
|
1 |
|
if ($this->hasOption($option)) { |
|
92
|
1 |
|
$result = array(true, call_user_func($callback, $option, $parameter)); |
|
93
|
|
|
} |
|
94
|
1 |
|
$results[$option] = $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
return $results; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return null|string |
|
102
|
|
|
*/ |
|
103
|
8 |
|
public function getFirstRemainingOption() |
|
104
|
|
|
{ |
|
105
|
|
|
/** @noinspection LoopWhichDoesNotLoopInspection */ |
|
106
|
8 |
|
foreach (new OptionIterator($this) as $option) { |
|
107
|
3 |
|
return $option; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
5 |
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* get option w/ optional argument |
|
115
|
|
|
* |
|
116
|
|
|
* optional by equal sign (--option=argument) |
|
117
|
|
|
* |
|
118
|
|
|
* @param string|string[] $option |
|
119
|
|
|
* @param null|bool|string $default [optional] argument |
|
120
|
|
|
* |
|
121
|
|
|
* @return null|bool|string null when not found, default when found with no argument or the argument |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function getOptionOptionalArgument($option, $default = null) |
|
124
|
|
|
{ |
|
125
|
1 |
|
$result = null; |
|
126
|
|
|
|
|
127
|
1 |
|
$options = new OptionIterator($this); |
|
128
|
1 |
|
$options->seekOption($option); |
|
129
|
1 |
|
if (!$options->currentMatchesOption($option)) { |
|
130
|
1 |
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
$buffer = $options->current(); |
|
134
|
1 |
|
$equalPos = strpos($buffer, '='); |
|
135
|
1 |
|
if (false === $equalPos) { |
|
136
|
1 |
|
$result = $default; |
|
137
|
|
|
} else { |
|
138
|
1 |
|
$result = (string)substr($buffer, $equalPos + 1); |
|
139
|
|
|
} |
|
140
|
1 |
|
unset($this->arguments[$options->key()]); |
|
141
|
|
|
|
|
142
|
1 |
|
return $result; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get the argument of an option. |
|
147
|
|
|
* |
|
148
|
|
|
* NOTE: returns only the first option value if multiple options would match |
|
149
|
|
|
* |
|
150
|
|
|
* @param string|string[] $option |
|
151
|
|
|
* @param null|bool|string $default [optional] |
|
152
|
|
|
* @param bool $required [optional] |
|
153
|
|
|
* |
|
154
|
|
|
* @throws InvalidArgumentException |
|
155
|
|
|
* @throws ArgsException |
|
156
|
|
|
* |
|
157
|
|
|
* @return null|bool|string |
|
158
|
|
|
*/ |
|
159
|
7 |
|
public function getOptionArgument($option, $default = null, $required = false) |
|
160
|
|
|
{ |
|
161
|
7 |
|
$result = null; |
|
162
|
|
|
|
|
163
|
|
|
/** @var OptionFilterIterator|OptionIterator $options */ |
|
164
|
7 |
|
$options = new OptionFilterIterator($this, $option); |
|
165
|
|
|
/** @noinspection LoopWhichDoesNotLoopInspection */ |
|
166
|
7 |
|
foreach ($options as $index => $argument) { |
|
167
|
|
|
/** @scrutinizer ignore-call */ |
|
168
|
3 |
|
$result = $options->getArgument(); |
|
169
|
1 |
|
unset($this->arguments[$index], $this->arguments[$index + 1]); |
|
170
|
|
|
|
|
171
|
1 |
|
break; # first match |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
5 |
|
if (null === $result) { |
|
175
|
4 |
|
if ($required) { |
|
176
|
1 |
|
throw new ArgsException(sprintf( |
|
177
|
1 |
|
'option %s is not optional', |
|
178
|
|
|
$options->/** @scrutinizer ignore-call */ |
|
179
|
1 |
|
getOptionDescription() |
|
180
|
|
|
)); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
3 |
|
$result = $default; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
return $result; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get the argument of an option. |
|
191
|
|
|
* |
|
192
|
|
|
* NOTE: returns only the first option value if multiple options would match |
|
193
|
|
|
* |
|
194
|
|
|
* @param string|string[] $option |
|
195
|
|
|
* @param string $default |
|
196
|
|
|
* @param bool $required [optional] |
|
197
|
|
|
* |
|
198
|
|
|
* @throws InvalidArgumentException |
|
199
|
|
|
* @throws ArgsException |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
1 |
|
public function getStringOptionArgument($option, $default, $required = false) |
|
204
|
|
|
{ |
|
205
|
1 |
|
if (!is_string($default)) { |
|
|
|
|
|
|
206
|
1 |
|
throw new InvalidArgumentException(sprintf('default value must be string, %s given', gettype($default))); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
1 |
|
return (string)$this->getOptionArgument($option, $default, $required); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function getUtility() |
|
216
|
|
|
{ |
|
217
|
1 |
|
return $this->utility; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
1 |
|
public function getRemaining() |
|
224
|
|
|
{ |
|
225
|
1 |
|
return $this->arguments; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|