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\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\InvalidOptionException;
18 * ArrayInput represents an input provided as an array.
22 * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
24 * @author Fabien Potencier <fabien@symfony.com>
26 class ArrayInput extends Input
33 * @param array $parameters An array of parameters
34 * @param InputDefinition|null $definition A InputDefinition instance
36 public function __construct(array $parameters, InputDefinition $definition = null)
38 $this->parameters = $parameters;
40 parent::__construct($definition);
46 public function getFirstArgument()
48 foreach ($this->parameters as $key => $value) {
49 if ($key && '-' === $key[0]) {
60 public function hasParameterOption($values, $onlyParams = false)
62 $values = (array) $values;
64 foreach ($this->parameters as $k => $v) {
69 if ($onlyParams && $v === '--') {
73 if (in_array($v, $values)) {
84 public function getParameterOption($values, $default = false, $onlyParams = false)
86 $values = (array) $values;
88 foreach ($this->parameters as $k => $v) {
89 if ($onlyParams && ($k === '--' || (is_int($k) && $v === '--'))) {
94 if (in_array($v, $values)) {
97 } elseif (in_array($k, $values)) {
106 * Returns a stringified representation of the args passed to the command.
110 public function __toString()
113 foreach ($this->parameters as $param => $val) {
114 if ($param && '-' === $param[0]) {
115 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
117 $params[] = $this->escapeToken($val);
121 return implode(' ', $params);
127 protected function parse()
129 foreach ($this->parameters as $key => $value) {
133 if (0 === strpos($key, '--')) {
134 $this->addLongOption(substr($key, 2), $value);
135 } elseif ('-' === $key[0]) {
136 $this->addShortOption(substr($key, 1), $value);
138 $this->addArgument($key, $value);
144 * Adds a short option value.
146 * @param string $shortcut The short option key
147 * @param mixed $value The value for the option
149 * @throws InvalidOptionException When option given doesn't exist
151 private function addShortOption($shortcut, $value)
153 if (!$this->definition->hasShortcut($shortcut)) {
154 throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
157 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
161 * Adds a long option value.
163 * @param string $name The long option key
164 * @param mixed $value The value for the option
166 * @throws InvalidOptionException When option given doesn't exist
167 * @throws InvalidOptionException When a required value is missing
169 private function addLongOption($name, $value)
171 if (!$this->definition->hasOption($name)) {
172 throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
175 $option = $this->definition->getOption($name);
177 if (null === $value) {
178 if ($option->isValueRequired()) {
179 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
182 $value = $option->isValueOptional() ? $option->getDefault() : true;
185 $this->options[$name] = $value;
189 * Adds an argument value.
191 * @param string $name The argument name
192 * @param mixed $value The value for the argument
194 * @throws InvalidArgumentException When argument given doesn't exist
196 private function addArgument($name, $value)
198 if (!$this->definition->hasArgument($name)) {
199 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
202 $this->arguments[$name] = $value;