4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Input;
14 use Symfony\Component\Console\Exception\RuntimeException;
17 * ArgvInput represents an input coming from the CLI arguments.
21 * $input = new ArgvInput();
23 * By default, the `$_SERVER['argv']` array is used for the input values.
25 * This can be overridden by explicitly passing the input values in the constructor:
27 * $input = new ArgvInput($_SERVER['argv']);
29 * If you pass it yourself, don't forget that the first element of the array
30 * is the name of the running application.
32 * When passing an argument to the constructor, be sure that it respects
33 * the same rules as the argv one. It's almost always better to use the
34 * `StringInput` when you want to provide your own input.
36 * @author Fabien Potencier <fabien@symfony.com>
38 * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
39 * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
41 class ArgvInput extends Input
49 * @param array|null $argv An array of parameters from the CLI (in the argv format)
50 * @param InputDefinition|null $definition A InputDefinition instance
52 public function __construct(array $argv = null, InputDefinition $definition = null)
55 $argv = $_SERVER['argv'];
58 // strip the application name
61 $this->tokens = $argv;
63 parent::__construct($definition);
66 protected function setTokens(array $tokens)
68 $this->tokens = $tokens;
74 protected function parse()
77 $this->parsed = $this->tokens;
78 while (null !== $token = array_shift($this->parsed)) {
79 if ($parseOptions && '' == $token) {
80 $this->parseArgument($token);
81 } elseif ($parseOptions && '--' == $token) {
82 $parseOptions = false;
83 } elseif ($parseOptions && 0 === strpos($token, '--')) {
84 $this->parseLongOption($token);
85 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
86 $this->parseShortOption($token);
88 $this->parseArgument($token);
94 * Parses a short option.
96 * @param string $token The current token
98 private function parseShortOption($token)
100 $name = substr($token, 1);
102 if (strlen($name) > 1) {
103 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
104 // an option with a value (with no space)
105 $this->addShortOption($name[0], substr($name, 1));
107 $this->parseShortOptionSet($name);
110 $this->addShortOption($name, null);
115 * Parses a short option set.
117 * @param string $name The current token
119 * @throws RuntimeException When option given doesn't exist
121 private function parseShortOptionSet($name)
123 $len = strlen($name);
124 for ($i = 0; $i < $len; ++$i) {
125 if (!$this->definition->hasShortcut($name[$i])) {
126 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
129 $option = $this->definition->getOptionForShortcut($name[$i]);
130 if ($option->acceptValue()) {
131 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
135 $this->addLongOption($option->getName(), null);
141 * Parses a long option.
143 * @param string $token The current token
145 private function parseLongOption($token)
147 $name = substr($token, 2);
149 if (false !== $pos = strpos($name, '=')) {
150 if (0 === strlen($value = substr($name, $pos + 1))) {
151 array_unshift($this->parsed, null);
153 $this->addLongOption(substr($name, 0, $pos), $value);
155 $this->addLongOption($name, null);
160 * Parses an argument.
162 * @param string $token The current token
164 * @throws RuntimeException When too many arguments are given
166 private function parseArgument($token)
168 $c = count($this->arguments);
170 // if input is expecting another argument, add it
171 if ($this->definition->hasArgument($c)) {
172 $arg = $this->definition->getArgument($c);
173 $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
175 // if last argument isArray(), append token to last argument
176 } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
177 $arg = $this->definition->getArgument($c - 1);
178 $this->arguments[$arg->getName()][] = $token;
180 // unexpected argument
182 $all = $this->definition->getArguments();
184 throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
187 throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token));
192 * Adds a short option value.
194 * @param string $shortcut The short option key
195 * @param mixed $value The value for the option
197 * @throws RuntimeException When option given doesn't exist
199 private function addShortOption($shortcut, $value)
201 if (!$this->definition->hasShortcut($shortcut)) {
202 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
205 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
209 * Adds a long option value.
211 * @param string $name The long option key
212 * @param mixed $value The value for the option
214 * @throws RuntimeException When option given doesn't exist
216 private function addLongOption($name, $value)
218 if (!$this->definition->hasOption($name)) {
219 throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
222 $option = $this->definition->getOption($name);
224 // Convert empty values to null
225 if (!isset($value[0])) {
229 if (null !== $value && !$option->acceptValue()) {
230 throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
233 if (null === $value && $option->acceptValue() && count($this->parsed)) {
234 // if option accepts an optional or mandatory argument
235 // let's see if there is one provided
236 $next = array_shift($this->parsed);
237 if (isset($next[0]) && '-' !== $next[0]) {
239 } elseif (empty($next)) {
242 array_unshift($this->parsed, $next);
246 if (null === $value) {
247 if ($option->isValueRequired()) {
248 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
251 if (!$option->isArray()) {
252 $value = $option->isValueOptional() ? $option->getDefault() : true;
256 if ($option->isArray()) {
257 $this->options[$name][] = $value;
259 $this->options[$name] = $value;
266 public function getFirstArgument()
268 foreach ($this->tokens as $token) {
269 if ($token && '-' === $token[0]) {
280 public function hasParameterOption($values)
282 $values = (array) $values;
284 foreach ($this->tokens as $token) {
285 foreach ($values as $value) {
286 if ($token === $value || 0 === strpos($token, $value.'=')) {
298 public function getParameterOption($values, $default = false)
300 $values = (array) $values;
301 $tokens = $this->tokens;
303 while (0 < count($tokens)) {
304 $token = array_shift($tokens);
306 foreach ($values as $value) {
307 if ($token === $value || 0 === strpos($token, $value.'=')) {
308 if (false !== $pos = strpos($token, '=')) {
309 return substr($token, $pos + 1);
312 return array_shift($tokens);
321 * Returns a stringified representation of the args passed to the command.
325 public function __toString()
328 $tokens = array_map(function ($token) use ($self) {
329 if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
330 return $match[1].$self->escapeToken($match[2]);
333 if ($token && $token[0] !== '-') {
334 return $self->escapeToken($token);
340 return implode(' ', $tokens);