comparison vendor/symfony/console/Input/ArgvInput.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
42 { 42 {
43 private $tokens; 43 private $tokens;
44 private $parsed; 44 private $parsed;
45 45
46 /** 46 /**
47 * Constructor.
48 *
49 * @param array|null $argv An array of parameters from the CLI (in the argv format) 47 * @param array|null $argv An array of parameters from the CLI (in the argv format)
50 * @param InputDefinition|null $definition A InputDefinition instance 48 * @param InputDefinition|null $definition A InputDefinition instance
51 */ 49 */
52 public function __construct(array $argv = null, InputDefinition $definition = null) 50 public function __construct(array $argv = null, InputDefinition $definition = null)
53 { 51 {
146 { 144 {
147 $name = substr($token, 2); 145 $name = substr($token, 2);
148 146
149 if (false !== $pos = strpos($name, '=')) { 147 if (false !== $pos = strpos($name, '=')) {
150 if (0 === strlen($value = substr($name, $pos + 1))) { 148 if (0 === strlen($value = substr($name, $pos + 1))) {
151 array_unshift($this->parsed, null); 149 // if no value after "=" then substr() returns "" since php7 only, false before
150 // see http://php.net/manual/fr/migration70.incompatible.php#119151
151 if (\PHP_VERSION_ID < 70000 && false === $value) {
152 $value = '';
153 }
154 array_unshift($this->parsed, $value);
152 } 155 }
153 $this->addLongOption(substr($name, 0, $pos), $value); 156 $this->addLongOption(substr($name, 0, $pos), $value);
154 } else { 157 } else {
155 $this->addLongOption($name, null); 158 $this->addLongOption($name, null);
156 } 159 }
219 throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); 222 throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
220 } 223 }
221 224
222 $option = $this->definition->getOption($name); 225 $option = $this->definition->getOption($name);
223 226
224 // Convert empty values to null
225 if (!isset($value[0])) {
226 $value = null;
227 }
228
229 if (null !== $value && !$option->acceptValue()) { 227 if (null !== $value && !$option->acceptValue()) {
230 throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); 228 throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
231 } 229 }
232 230
233 if (null === $value && $option->acceptValue() && count($this->parsed)) { 231 if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
234 // if option accepts an optional or mandatory argument 232 // if option accepts an optional or mandatory argument
235 // let's see if there is one provided 233 // let's see if there is one provided
236 $next = array_shift($this->parsed); 234 $next = array_shift($this->parsed);
237 if (isset($next[0]) && '-' !== $next[0]) { 235 if ((isset($next[0]) && '-' !== $next[0]) || in_array($next, array('', null), true)) {
238 $value = $next; 236 $value = $next;
239 } elseif (empty($next)) {
240 $value = null;
241 } else { 237 } else {
242 array_unshift($this->parsed, $next); 238 array_unshift($this->parsed, $next);
243 } 239 }
244 } 240 }
245 241
246 if (null === $value) { 242 if (null === $value) {
247 if ($option->isValueRequired()) { 243 if ($option->isValueRequired()) {
248 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); 244 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
249 } 245 }
250 246
251 if (!$option->isArray()) { 247 if (!$option->isArray() && !$option->isValueOptional()) {
252 $value = $option->isValueOptional() ? $option->getDefault() : true; 248 $value = true;
253 } 249 }
254 } 250 }
255 251
256 if ($option->isArray()) { 252 if ($option->isArray()) {
257 $this->options[$name][] = $value; 253 $this->options[$name][] = $value;
280 public function hasParameterOption($values, $onlyParams = false) 276 public function hasParameterOption($values, $onlyParams = false)
281 { 277 {
282 $values = (array) $values; 278 $values = (array) $values;
283 279
284 foreach ($this->tokens as $token) { 280 foreach ($this->tokens as $token) {
285 if ($onlyParams && $token === '--') { 281 if ($onlyParams && '--' === $token) {
286 return false; 282 return false;
287 } 283 }
288 foreach ($values as $value) { 284 foreach ($values as $value) {
289 if ($token === $value || 0 === strpos($token, $value.'=')) { 285 // Options with values:
286 // For long options, test for '--option=' at beginning
287 // For short options, test for '-o' at beginning
288 $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
289 if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) {
290 return true; 290 return true;
291 } 291 }
292 } 292 }
293 } 293 }
294 294
303 $values = (array) $values; 303 $values = (array) $values;
304 $tokens = $this->tokens; 304 $tokens = $this->tokens;
305 305
306 while (0 < count($tokens)) { 306 while (0 < count($tokens)) {
307 $token = array_shift($tokens); 307 $token = array_shift($tokens);
308 if ($onlyParams && $token === '--') { 308 if ($onlyParams && '--' === $token) {
309 return false; 309 return false;
310 } 310 }
311 311
312 foreach ($values as $value) { 312 foreach ($values as $value) {
313 if ($token === $value || 0 === strpos($token, $value.'=')) { 313 if ($token === $value) {
314 if (false !== $pos = strpos($token, '=')) {
315 return substr($token, $pos + 1);
316 }
317
318 return array_shift($tokens); 314 return array_shift($tokens);
315 }
316 // Options with values:
317 // For long options, test for '--option=' at beginning
318 // For short options, test for '-o' at beginning
319 $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
320 if ('' !== $leading && 0 === strpos($token, $leading)) {
321 return substr($token, strlen($leading));
319 } 322 }
320 } 323 }
321 } 324 }
322 325
323 return $default; 326 return $default;
333 $tokens = array_map(function ($token) { 336 $tokens = array_map(function ($token) {
334 if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { 337 if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
335 return $match[1].$this->escapeToken($match[2]); 338 return $match[1].$this->escapeToken($match[2]);
336 } 339 }
337 340
338 if ($token && $token[0] !== '-') { 341 if ($token && '-' !== $token[0]) {
339 return $this->escapeToken($token); 342 return $this->escapeToken($token);
340 } 343 }
341 344
342 return $token; 345 return $token;
343 }, $this->tokens); 346 }, $this->tokens);