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)
62 $values = (array) $values;
64 foreach ($this->parameters as $k => $v) {
69 if (in_array($v, $values)) {
80 public function getParameterOption($values, $default = false)
82 $values = (array) $values;
84 foreach ($this->parameters as $k => $v) {
86 if (in_array($v, $values)) {
89 } elseif (in_array($k, $values)) {
98 * Returns a stringified representation of the args passed to the command.
102 public function __toString()
105 foreach ($this->parameters as $param => $val) {
106 if ($param && '-' === $param[0]) {
107 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
109 $params[] = $this->escapeToken($val);
113 return implode(' ', $params);
119 protected function parse()
121 foreach ($this->parameters as $key => $value) {
122 if (0 === strpos($key, '--')) {
123 $this->addLongOption(substr($key, 2), $value);
124 } elseif ('-' === $key[0]) {
125 $this->addShortOption(substr($key, 1), $value);
127 $this->addArgument($key, $value);
133 * Adds a short option value.
135 * @param string $shortcut The short option key
136 * @param mixed $value The value for the option
138 * @throws InvalidOptionException When option given doesn't exist
140 private function addShortOption($shortcut, $value)
142 if (!$this->definition->hasShortcut($shortcut)) {
143 throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
146 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
150 * Adds a long option value.
152 * @param string $name The long option key
153 * @param mixed $value The value for the option
155 * @throws InvalidOptionException When option given doesn't exist
156 * @throws InvalidOptionException When a required value is missing
158 private function addLongOption($name, $value)
160 if (!$this->definition->hasOption($name)) {
161 throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
164 $option = $this->definition->getOption($name);
166 if (null === $value) {
167 if ($option->isValueRequired()) {
168 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
171 $value = $option->isValueOptional() ? $option->getDefault() : true;
174 $this->options[$name] = $value;
178 * Adds an argument value.
180 * @param string $name The argument name
181 * @param mixed $value The value for the argument
183 * @throws InvalidArgumentException When argument given doesn't exist
185 private function addArgument($name, $value)
187 if (!$this->definition->hasArgument($name)) {
188 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
191 $this->arguments[$name] = $value;