Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Input; Chris@0: Chris@0: use Symfony\Component\Console\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * ArgvInput represents an input coming from the CLI arguments. Chris@0: * Chris@0: * Usage: Chris@0: * Chris@0: * $input = new ArgvInput(); Chris@0: * Chris@0: * By default, the `$_SERVER['argv']` array is used for the input values. Chris@0: * Chris@0: * This can be overridden by explicitly passing the input values in the constructor: Chris@0: * Chris@0: * $input = new ArgvInput($_SERVER['argv']); Chris@0: * Chris@0: * If you pass it yourself, don't forget that the first element of the array Chris@0: * is the name of the running application. Chris@0: * Chris@0: * When passing an argument to the constructor, be sure that it respects Chris@0: * the same rules as the argv one. It's almost always better to use the Chris@0: * `StringInput` when you want to provide your own input. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * Chris@0: * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html Chris@0: * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02 Chris@0: */ Chris@0: class ArgvInput extends Input Chris@0: { Chris@0: private $tokens; Chris@0: private $parsed; Chris@0: Chris@0: /** Chris@0: * @param array|null $argv An array of parameters from the CLI (in the argv format) Chris@0: * @param InputDefinition|null $definition A InputDefinition instance Chris@0: */ Chris@0: public function __construct(array $argv = null, InputDefinition $definition = null) Chris@0: { Chris@0: if (null === $argv) { Chris@0: $argv = $_SERVER['argv']; Chris@0: } Chris@0: Chris@0: // strip the application name Chris@0: array_shift($argv); Chris@0: Chris@0: $this->tokens = $argv; Chris@0: Chris@0: parent::__construct($definition); Chris@0: } Chris@0: Chris@0: protected function setTokens(array $tokens) Chris@0: { Chris@0: $this->tokens = $tokens; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function parse() Chris@0: { Chris@0: $parseOptions = true; Chris@0: $this->parsed = $this->tokens; Chris@0: while (null !== $token = array_shift($this->parsed)) { Chris@0: if ($parseOptions && '' == $token) { Chris@0: $this->parseArgument($token); Chris@0: } elseif ($parseOptions && '--' == $token) { Chris@0: $parseOptions = false; Chris@0: } elseif ($parseOptions && 0 === strpos($token, '--')) { Chris@0: $this->parseLongOption($token); Chris@0: } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { Chris@0: $this->parseShortOption($token); Chris@0: } else { Chris@0: $this->parseArgument($token); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a short option. Chris@0: * Chris@0: * @param string $token The current token Chris@0: */ Chris@0: private function parseShortOption($token) Chris@0: { Chris@0: $name = substr($token, 1); Chris@0: Chris@17: if (\strlen($name) > 1) { Chris@0: if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { Chris@0: // an option with a value (with no space) Chris@0: $this->addShortOption($name[0], substr($name, 1)); Chris@0: } else { Chris@0: $this->parseShortOptionSet($name); Chris@0: } Chris@0: } else { Chris@0: $this->addShortOption($name, null); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a short option set. Chris@0: * Chris@0: * @param string $name The current token Chris@0: * Chris@0: * @throws RuntimeException When option given doesn't exist Chris@0: */ Chris@0: private function parseShortOptionSet($name) Chris@0: { Chris@17: $len = \strlen($name); Chris@0: for ($i = 0; $i < $len; ++$i) { Chris@0: if (!$this->definition->hasShortcut($name[$i])) { Chris@17: $encoding = mb_detect_encoding($name, null, true); Chris@17: throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); Chris@0: } Chris@0: Chris@0: $option = $this->definition->getOptionForShortcut($name[$i]); Chris@0: if ($option->acceptValue()) { Chris@0: $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); Chris@0: Chris@0: break; Chris@0: } else { Chris@0: $this->addLongOption($option->getName(), null); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a long option. Chris@0: * Chris@0: * @param string $token The current token Chris@0: */ Chris@0: private function parseLongOption($token) Chris@0: { Chris@0: $name = substr($token, 2); Chris@0: Chris@0: if (false !== $pos = strpos($name, '=')) { Chris@17: if (0 === \strlen($value = substr($name, $pos + 1))) { Chris@14: // if no value after "=" then substr() returns "" since php7 only, false before Chris@14: // see http://php.net/manual/fr/migration70.incompatible.php#119151 Chris@14: if (\PHP_VERSION_ID < 70000 && false === $value) { Chris@14: $value = ''; Chris@14: } Chris@14: array_unshift($this->parsed, $value); Chris@0: } Chris@0: $this->addLongOption(substr($name, 0, $pos), $value); Chris@0: } else { Chris@0: $this->addLongOption($name, null); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses an argument. Chris@0: * Chris@0: * @param string $token The current token Chris@0: * Chris@0: * @throws RuntimeException When too many arguments are given Chris@0: */ Chris@0: private function parseArgument($token) Chris@0: { Chris@17: $c = \count($this->arguments); Chris@0: Chris@0: // if input is expecting another argument, add it Chris@0: if ($this->definition->hasArgument($c)) { Chris@0: $arg = $this->definition->getArgument($c); Chris@17: $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; Chris@0: Chris@0: // if last argument isArray(), append token to last argument Chris@0: } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { Chris@0: $arg = $this->definition->getArgument($c - 1); Chris@0: $this->arguments[$arg->getName()][] = $token; Chris@0: Chris@0: // unexpected argument Chris@0: } else { Chris@0: $all = $this->definition->getArguments(); Chris@17: if (\count($all)) { Chris@0: throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all)))); Chris@0: } Chris@0: Chris@0: throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a short option value. Chris@0: * Chris@0: * @param string $shortcut The short option key Chris@0: * @param mixed $value The value for the option Chris@0: * Chris@0: * @throws RuntimeException When option given doesn't exist Chris@0: */ Chris@0: private function addShortOption($shortcut, $value) Chris@0: { Chris@0: if (!$this->definition->hasShortcut($shortcut)) { Chris@0: throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); Chris@0: } Chris@0: Chris@0: $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a long option value. Chris@0: * Chris@0: * @param string $name The long option key Chris@0: * @param mixed $value The value for the option Chris@0: * Chris@0: * @throws RuntimeException When option given doesn't exist Chris@0: */ Chris@0: private function addLongOption($name, $value) Chris@0: { Chris@0: if (!$this->definition->hasOption($name)) { Chris@0: throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); Chris@0: } Chris@0: Chris@0: $option = $this->definition->getOption($name); Chris@0: Chris@0: if (null !== $value && !$option->acceptValue()) { Chris@0: throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); Chris@0: } Chris@0: Chris@17: if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { Chris@0: // if option accepts an optional or mandatory argument Chris@0: // let's see if there is one provided Chris@0: $next = array_shift($this->parsed); Chris@17: if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) { Chris@0: $value = $next; Chris@0: } else { Chris@0: array_unshift($this->parsed, $next); Chris@0: } Chris@0: } Chris@0: Chris@0: if (null === $value) { Chris@0: if ($option->isValueRequired()) { Chris@0: throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); Chris@0: } Chris@0: Chris@14: if (!$option->isArray() && !$option->isValueOptional()) { Chris@14: $value = true; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($option->isArray()) { Chris@0: $this->options[$name][] = $value; Chris@0: } else { Chris@0: $this->options[$name] = $value; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFirstArgument() Chris@0: { Chris@18: $isOption = false; Chris@18: foreach ($this->tokens as $i => $token) { Chris@0: if ($token && '-' === $token[0]) { Chris@18: if (false !== strpos($token, '=') || !isset($this->tokens[$i + 1])) { Chris@18: continue; Chris@18: } Chris@18: Chris@18: // If it's a long option, consider that everything after "--" is the option name. Chris@18: // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) Chris@18: $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); Chris@18: if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { Chris@18: // noop Chris@18: } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { Chris@18: $isOption = true; Chris@18: } Chris@18: Chris@18: continue; Chris@18: } Chris@18: Chris@18: if ($isOption) { Chris@18: $isOption = false; Chris@0: continue; Chris@0: } Chris@0: Chris@0: return $token; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasParameterOption($values, $onlyParams = false) Chris@0: { Chris@0: $values = (array) $values; Chris@0: Chris@0: foreach ($this->tokens as $token) { Chris@14: if ($onlyParams && '--' === $token) { Chris@0: return false; Chris@0: } Chris@0: foreach ($values as $value) { Chris@14: // Options with values: Chris@14: // For long options, test for '--option=' at beginning Chris@14: // For short options, test for '-o' at beginning Chris@14: $leading = 0 === strpos($value, '--') ? $value.'=' : $value; Chris@14: if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getParameterOption($values, $default = false, $onlyParams = false) Chris@0: { Chris@0: $values = (array) $values; Chris@0: $tokens = $this->tokens; Chris@0: Chris@17: while (0 < \count($tokens)) { Chris@0: $token = array_shift($tokens); Chris@14: if ($onlyParams && '--' === $token) { Chris@17: return $default; Chris@0: } Chris@0: Chris@0: foreach ($values as $value) { Chris@14: if ($token === $value) { Chris@0: return array_shift($tokens); Chris@0: } Chris@14: // Options with values: Chris@14: // For long options, test for '--option=' at beginning Chris@14: // For short options, test for '-o' at beginning Chris@14: $leading = 0 === strpos($value, '--') ? $value.'=' : $value; Chris@14: if ('' !== $leading && 0 === strpos($token, $leading)) { Chris@17: return substr($token, \strlen($leading)); Chris@14: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $default; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a stringified representation of the args passed to the command. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@0: $tokens = array_map(function ($token) { Chris@0: if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { Chris@0: return $match[1].$this->escapeToken($match[2]); Chris@0: } Chris@0: Chris@14: if ($token && '-' !== $token[0]) { Chris@0: return $this->escapeToken($token); Chris@0: } Chris@0: Chris@0: return $token; Chris@0: }, $this->tokens); Chris@0: Chris@0: return implode(' ', $tokens); Chris@0: } Chris@0: }