123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- namespace Symfony\Component\Console\Input;
- use Symfony\Component\Console\Exception\RuntimeException;
- class ArgvInput extends Input
- {
- private $tokens;
- private $parsed;
-
- public function __construct(array $argv = null, InputDefinition $definition = null)
- {
- if (null === $argv) {
- $argv = $_SERVER['argv'];
- }
-
- array_shift($argv);
- $this->tokens = $argv;
- parent::__construct($definition);
- }
- protected function setTokens(array $tokens)
- {
- $this->tokens = $tokens;
- }
-
- protected function parse()
- {
- $parseOptions = true;
- $this->parsed = $this->tokens;
- while (null !== $token = array_shift($this->parsed)) {
- if ($parseOptions && '' == $token) {
- $this->parseArgument($token);
- } elseif ($parseOptions && '--' == $token) {
- $parseOptions = false;
- } elseif ($parseOptions && 0 === strpos($token, '--')) {
- $this->parseLongOption($token);
- } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
- $this->parseShortOption($token);
- } else {
- $this->parseArgument($token);
- }
- }
- }
-
- private function parseShortOption($token)
- {
- $name = substr($token, 1);
- if (\strlen($name) > 1) {
- if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
-
- $this->addShortOption($name[0], substr($name, 1));
- } else {
- $this->parseShortOptionSet($name);
- }
- } else {
- $this->addShortOption($name, null);
- }
- }
-
- private function parseShortOptionSet($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]));
- }
- $option = $this->definition->getOptionForShortcut($name[$i]);
- if ($option->acceptValue()) {
- $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
- break;
- } else {
- $this->addLongOption($option->getName(), null);
- }
- }
- }
-
- private function parseLongOption($token)
- {
- $name = substr($token, 2);
- if (false !== $pos = strpos($name, '=')) {
- if (0 === \strlen($value = substr($name, $pos + 1))) {
-
-
- if (\PHP_VERSION_ID < 70000 && false === $value) {
- $value = '';
- }
- array_unshift($this->parsed, $value);
- }
- $this->addLongOption(substr($name, 0, $pos), $value);
- } else {
- $this->addLongOption($name, null);
- }
- }
-
- private function parseArgument($token)
- {
- $c = \count($this->arguments);
-
- if ($this->definition->hasArgument($c)) {
- $arg = $this->definition->getArgument($c);
- $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
-
- } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
- $arg = $this->definition->getArgument($c - 1);
- $this->arguments[$arg->getName()][] = $token;
-
- } else {
- $all = $this->definition->getArguments();
- if (\count($all)) {
- throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
- }
- throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token));
- }
- }
-
- private function addShortOption($shortcut, $value)
- {
- if (!$this->definition->hasShortcut($shortcut)) {
- throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
- }
- $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
- }
-
- private function addLongOption($name, $value)
- {
- if (!$this->definition->hasOption($name)) {
- throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
- }
- $option = $this->definition->getOption($name);
- if (null !== $value && !$option->acceptValue()) {
- throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
- }
- if (\in_array($value, array('', null), true) && $option->acceptValue() && \count($this->parsed)) {
-
-
- $next = array_shift($this->parsed);
- if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, array('', null), true)) {
- $value = $next;
- } else {
- array_unshift($this->parsed, $next);
- }
- }
- if (null === $value) {
- if ($option->isValueRequired()) {
- throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
- }
- if (!$option->isArray() && !$option->isValueOptional()) {
- $value = true;
- }
- }
- if ($option->isArray()) {
- $this->options[$name][] = $value;
- } else {
- $this->options[$name] = $value;
- }
- }
-
- public function getFirstArgument()
- {
- foreach ($this->tokens as $token) {
- if ($token && '-' === $token[0]) {
- continue;
- }
- return $token;
- }
- }
-
- public function hasParameterOption($values, $onlyParams = false)
- {
- $values = (array) $values;
- foreach ($this->tokens as $token) {
- if ($onlyParams && '--' === $token) {
- return false;
- }
- foreach ($values as $value) {
-
-
-
- $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
- if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) {
- return true;
- }
- }
- }
- return false;
- }
-
- public function getParameterOption($values, $default = false, $onlyParams = false)
- {
- $values = (array) $values;
- $tokens = $this->tokens;
- while (0 < \count($tokens)) {
- $token = array_shift($tokens);
- if ($onlyParams && '--' === $token) {
- return $default;
- }
- foreach ($values as $value) {
- if ($token === $value) {
- return array_shift($tokens);
- }
-
-
-
- $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
- if ('' !== $leading && 0 === strpos($token, $leading)) {
- return substr($token, \strlen($leading));
- }
- }
- }
- return $default;
- }
-
- public function __toString()
- {
- $tokens = array_map(function ($token) {
- if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
- return $match[1].$this->escapeToken($match[2]);
- }
- if ($token && '-' !== $token[0]) {
- return $this->escapeToken($token);
- }
- return $token;
- }, $this->tokens);
- return implode(' ', $tokens);
- }
- }
|