private $parsed;
/**
- * Constructor.
- *
* @param array|null $argv An array of parameters from the CLI (in the argv format)
* @param InputDefinition|null $definition A InputDefinition instance
*/
if (false !== $pos = strpos($name, '=')) {
if (0 === strlen($value = substr($name, $pos + 1))) {
- array_unshift($this->parsed, null);
+ // if no value after "=" then substr() returns "" since php7 only, false before
+ // see http://php.net/manual/fr/migration70.incompatible.php#119151
+ if (\PHP_VERSION_ID < 70000 && false === $value) {
+ $value = '';
+ }
+ array_unshift($this->parsed, $value);
}
$this->addLongOption(substr($name, 0, $pos), $value);
} else {
$option = $this->definition->getOption($name);
- // Convert empty values to null
- if (!isset($value[0])) {
- $value = null;
- }
-
if (null !== $value && !$option->acceptValue()) {
throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
}
- if (null === $value && $option->acceptValue() && count($this->parsed)) {
+ if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
// if option accepts an optional or mandatory argument
// let's see if there is one provided
$next = array_shift($this->parsed);
- if (isset($next[0]) && '-' !== $next[0]) {
+ if ((isset($next[0]) && '-' !== $next[0]) || in_array($next, array('', null), true)) {
$value = $next;
- } elseif (empty($next)) {
- $value = null;
} else {
array_unshift($this->parsed, $next);
}
throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
}
- if (!$option->isArray()) {
- $value = $option->isValueOptional() ? $option->getDefault() : true;
+ if (!$option->isArray() && !$option->isValueOptional()) {
+ $value = true;
}
}
$values = (array) $values;
foreach ($this->tokens as $token) {
- if ($onlyParams && $token === '--') {
+ if ($onlyParams && '--' === $token) {
return false;
}
foreach ($values as $value) {
- if ($token === $value || 0 === strpos($token, $value.'=')) {
+ // Options with values:
+ // For long options, test for '--option=' at beginning
+ // For short options, test for '-o' at beginning
+ $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
+ if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) {
return true;
}
}
while (0 < count($tokens)) {
$token = array_shift($tokens);
- if ($onlyParams && $token === '--') {
+ if ($onlyParams && '--' === $token) {
return false;
}
foreach ($values as $value) {
- if ($token === $value || 0 === strpos($token, $value.'=')) {
- if (false !== $pos = strpos($token, '=')) {
- return substr($token, $pos + 1);
- }
-
+ if ($token === $value) {
return array_shift($tokens);
}
+ // Options with values:
+ // For long options, test for '--option=' at beginning
+ // For short options, test for '-o' at beginning
+ $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
+ if ('' !== $leading && 0 === strpos($token, $leading)) {
+ return substr($token, strlen($leading));
+ }
}
}
return $match[1].$this->escapeToken($match[2]);
}
- if ($token && $token[0] !== '-') {
+ if ($token && '-' !== $token[0]) {
return $this->escapeToken($token);
}