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;
15 * ArrayInput represents an input provided as an array.
19 * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
21 * @author Fabien Potencier <fabien@symfony.com>
25 class ArrayInput extends Input
32 * @param array $parameters An array of parameters
33 * @param InputDefinition $definition A InputDefinition instance
37 public function __construct(array $parameters, InputDefinition $definition = null)
39 $this->parameters = $parameters;
41 parent::__construct($definition);
45 * Returns the first argument from the raw parameters (not parsed).
47 * @return string The value of the first argument or null otherwise
49 public function getFirstArgument()
51 foreach ($this->parameters as $key => $value) {
52 if ($key && '-' === $key[0]) {
61 * Returns true if the raw parameters (not parsed) contain a value.
63 * This method is to be used to introspect the input parameters
64 * before they have been validated. It must be used carefully.
66 * @param string|array $values The values to look for in the raw parameters (can be an array)
68 * @return Boolean true if the value is contained in the raw parameters
70 public function hasParameterOption($values)
72 $values = (array) $values;
74 foreach ($this->parameters as $k => $v) {
79 if (in_array($v, $values)) {
88 * Returns the value of a raw option (not parsed).
90 * This method is to be used to introspect the input parameters
91 * before they have been validated. It must be used carefully.
93 * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
94 * @param mixed $default The default value to return if no result is found
96 * @return mixed The option value
98 public function getParameterOption($values, $default = false)
100 $values = (array) $values;
102 foreach ($this->parameters as $k => $v) {
103 if (is_int($k) && in_array($v, $values)) {
105 } elseif (in_array($k, $values)) {
114 * Processes command line arguments.
116 protected function parse()
118 foreach ($this->parameters as $key => $value) {
119 if (0 === strpos($key, '--')) {
120 $this->addLongOption(substr($key, 2), $value);
121 } elseif ('-' === $key[0]) {
122 $this->addShortOption(substr($key, 1), $value);
124 $this->addArgument($key, $value);
130 * Adds a short option value.
132 * @param string $shortcut The short option key
133 * @param mixed $value The value for the option
135 * @throws \InvalidArgumentException When option given doesn't exist
137 private function addShortOption($shortcut, $value)
139 if (!$this->definition->hasShortcut($shortcut)) {
140 throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
143 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
147 * Adds a long option value.
149 * @param string $name The long option key
150 * @param mixed $value The value for the option
152 * @throws \InvalidArgumentException When option given doesn't exist
153 * @throws \InvalidArgumentException When a required value is missing
155 private function addLongOption($name, $value)
157 if (!$this->definition->hasOption($name)) {
158 throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
161 $option = $this->definition->getOption($name);
163 if (null === $value) {
164 if ($option->isValueRequired()) {
165 throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
168 $value = $option->isValueOptional() ? $option->getDefault() : true;
171 $this->options[$name] = $value;
175 * Adds an argument value.
177 * @param string $name The argument name
178 * @param mixed $value The value for the argument
180 * @throws \InvalidArgumentException When argument given doesn't exist
182 private function addArgument($name, $value)
184 if (!$this->definition->hasArgument($name)) {
185 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
188 $this->arguments[$name] = $value;