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
*/
{
$name = substr($token, 1);
- if (strlen($name) > 1) {
+ if (\strlen($name) > 1) {
if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
// an option with a value (with no space)
$this->addShortOption($name[0], substr($name, 1));
*/
private function parseShortOptionSet($name)
{
- $len = strlen($name);
+ $len = \strlen($name);
for ($i = 0; $i < $len; ++$i) {
if (!$this->definition->hasShortcut($name[$i])) {
- throw new RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
+ $encoding = mb_detect_encoding($name, null, true);
+ throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding)));
}
$option = $this->definition->getOptionForShortcut($name[$i]);
$name = substr($token, 2);
if (false !== $pos = strpos($name, '=')) {
- if (0 === strlen($value = substr($name, $pos + 1))) {
- array_unshift($this->parsed, null);
+ if (0 === \strlen($value = substr($name, $pos + 1))) {
+ // 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 {
*/
private function parseArgument($token)
{
- $c = count($this->arguments);
+ $c = \count($this->arguments);
// if input is expecting another argument, add it
if ($this->definition->hasArgument($c)) {
// unexpected argument
} else {
$all = $this->definition->getArguments();
- if (count($all)) {
+ if (\count($all)) {
throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
}
$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;
}
}
$values = (array) $values;
$tokens = $this->tokens;
- while (0 < count($tokens)) {
+ while (0 < \count($tokens)) {
$token = array_shift($tokens);
- if ($onlyParams && $token === '--') {
- return false;
+ if ($onlyParams && '--' === $token) {
+ return $default;
}
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);
}