5 * A copy of ArgvInput (v3.4.2) while commenting out a section of hasParameterOption() per https://github.com/symfony/symfony/issues/25825.
7 * This file is part of the Symfony package.
9 * (c) Fabien Potencier <fabien@symfony.com>
11 * For the full copyright and license information, please view the LICENSE
12 * file that was distributed with this source code.
15 namespace Drush\Symfony;
17 use Symfony\Component\Console\Exception\RuntimeException;
18 use Symfony\Component\Console\Input\Input;
19 use Symfony\Component\Console\Input\ArgvInput;
20 use Symfony\Component\Console\Input\InputDefinition;
23 * ArgvInput represents an input coming from the CLI arguments.
27 * $input = new ArgvInput();
29 * By default, the `$_SERVER['argv']` array is used for the input values.
31 * This can be overridden by explicitly passing the input values in the constructor:
33 * $input = new ArgvInput($_SERVER['argv']);
35 * If you pass it yourself, don't forget that the first element of the array
36 * is the name of the running application.
38 * When passing an argument to the constructor, be sure that it respects
39 * the same rules as the argv one. It's almost always better to use the
40 * `StringInput` when you want to provide your own input.
42 * @author Fabien Potencier <fabien@symfony.com>
44 * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
45 * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
47 class DrushArgvInput extends ArgvInput
53 * @param array|null $argv An array of parameters from the CLI (in the argv format)
54 * @param InputDefinition|null $definition A InputDefinition instance
56 public function __construct(array $argv = null, InputDefinition $definition = null)
59 $argv = $_SERVER['argv'];
62 // strip the application name
65 $this->tokens = $argv;
67 parent::__construct($definition);
70 protected function setTokens(array $tokens)
72 $this->tokens = $tokens;
78 protected function parse()
81 $this->parsed = $this->tokens;
82 while (null !== $token = array_shift($this->parsed)) {
83 if ($parseOptions && '' == $token) {
84 $this->parseArgument($token);
85 } elseif ($parseOptions && '--' == $token) {
86 $parseOptions = false;
87 } elseif ($parseOptions && 0 === strpos($token, '--')) {
88 $this->parseLongOption($token);
89 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
90 $this->parseShortOption($token);
92 $this->parseArgument($token);
98 * Parses a short option.
100 * @param string $token The current token
102 private function parseShortOption($token)
104 $name = substr($token, 1);
106 if (strlen($name) > 1) {
107 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
108 // an option with a value (with no space)
109 $this->addShortOption($name[0], substr($name, 1));
111 $this->parseShortOptionSet($name);
114 $this->addShortOption($name, null);
119 * Parses a short option set.
121 * @param string $name The current token
123 * @throws RuntimeException When option given doesn't exist
125 private function parseShortOptionSet($name)
127 $len = strlen($name);
128 for ($i = 0; $i < $len; ++$i) {
129 if (!$this->definition->hasShortcut($name[$i])) {
130 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
133 $option = $this->definition->getOptionForShortcut($name[$i]);
134 if ($option->acceptValue()) {
135 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
139 $this->addLongOption($option->getName(), null);
145 * Parses a long option.
147 * @param string $token The current token
149 private function parseLongOption($token)
151 $name = substr($token, 2);
153 if (false !== $pos = strpos($name, '=')) {
154 if (0 === strlen($value = substr($name, $pos + 1))) {
155 // if no value after "=" then substr() returns "" since php7 only, false before
156 // see http://php.net/manual/fr/migration70.incompatible.php#119151
157 if (\PHP_VERSION_ID < 70000 && false === $value) {
160 array_unshift($this->parsed, $value);
162 $this->addLongOption(substr($name, 0, $pos), $value);
164 $this->addLongOption($name, null);
169 * Parses an argument.
171 * @param string $token The current token
173 * @throws RuntimeException When too many arguments are given
175 private function parseArgument($token)
177 $c = count($this->arguments);
179 // if input is expecting another argument, add it
180 if ($this->definition->hasArgument($c)) {
181 $arg = $this->definition->getArgument($c);
182 $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
184 // if last argument isArray(), append token to last argument
185 } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
186 $arg = $this->definition->getArgument($c - 1);
187 $this->arguments[$arg->getName()][] = $token;
189 // unexpected argument
191 $all = $this->definition->getArguments();
193 throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
196 throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token));
201 * Adds a short option value.
203 * @param string $shortcut The short option key
204 * @param mixed $value The value for the option
206 * @throws RuntimeException When option given doesn't exist
208 private function addShortOption($shortcut, $value)
210 if (!$this->definition->hasShortcut($shortcut)) {
211 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
214 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
218 * Adds a long option value.
220 * @param string $name The long option key
221 * @param mixed $value The value for the option
223 * @throws RuntimeException When option given doesn't exist
225 private function addLongOption($name, $value)
227 if (!$this->definition->hasOption($name)) {
228 throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
231 $option = $this->definition->getOption($name);
233 if (null !== $value && !$option->acceptValue()) {
234 throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
237 if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
238 // if option accepts an optional or mandatory argument
239 // let's see if there is one provided
240 $next = array_shift($this->parsed);
241 if ((isset($next[0]) && '-' !== $next[0]) || in_array($next, array('', null), true)) {
244 array_unshift($this->parsed, $next);
248 if (null === $value) {
249 if ($option->isValueRequired()) {
250 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
253 if (!$option->isArray() && !$option->isValueOptional()) {
258 if ($option->isArray()) {
259 $this->options[$name][] = $value;
261 $this->options[$name] = $value;
268 public function getFirstArgument()
270 foreach ($this->tokens as $token) {
271 if ($token && '-' === $token[0]) {
282 public function hasParameterOption($values, $onlyParams = false)
284 $values = (array) $values;
286 foreach ($this->tokens as $token) {
287 if ($onlyParams && '--' === $token) {
290 foreach ($values as $value) {
291 if ($token === $value || 0 === strpos($token, $value.'=')) {
296 * Commented out until https://github.com/symfony/symfony/issues/25825 is resolved and released.
298 // if (0 === strpos($token, '-') && 0 !== strpos($token, '--')) {
299 // $noValue = explode('=', $token);
300 // $token = $noValue[0];
301 // $searchableToken = str_replace('-', '', $token);
302 // $searchableValue = str_replace('-', '', $value);
303 // if ('' !== $searchableToken && '' !== $searchableValue && false !== strpos($searchableToken, $searchableValue)) {
316 public function getParameterOption($values, $default = false, $onlyParams = false)
318 $values = (array) $values;
319 $tokens = $this->tokens;
321 while (0 < count($tokens)) {
322 $token = array_shift($tokens);
323 if ($onlyParams && '--' === $token) {
327 foreach ($values as $value) {
328 if ($token === $value || 0 === strpos($token, $value.'=')) {
329 if (false !== $pos = strpos($token, '=')) {
330 return substr($token, $pos + 1);
333 return array_shift($tokens);
342 * Returns a stringified representation of the args passed to the command.
346 public function __toString()
348 $tokens = array_map(function ($token) {
349 if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
350 return $match[1].$this->escapeToken($match[2]);
353 if ($token && '-' !== $token[0]) {
354 return $this->escapeToken($token);
360 return implode(' ', $tokens);