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 * Input is the base class for all concrete Input classes.
17 * Three concrete classes are provided by default:
19 * * `ArgvInput`: The input comes from the CLI arguments (argv)
20 * * `StringInput`: The input is provided as a string
21 * * `ArrayInput`: The input is provided as an array
23 * @author Fabien Potencier <fabien@symfony.com>
25 abstract class Input implements InputInterface
27 protected $definition;
30 protected $interactive = true;
35 * @param InputDefinition $definition A InputDefinition instance
37 public function __construct(InputDefinition $definition = null)
39 if (null === $definition) {
40 $this->definition = new InputDefinition();
42 $this->bind($definition);
48 * Binds the current Input instance with the given arguments and options.
50 * @param InputDefinition $definition A InputDefinition instance
52 public function bind(InputDefinition $definition)
54 $this->arguments = array();
55 $this->options = array();
56 $this->definition = $definition;
62 * Processes command line arguments.
64 abstract protected function parse();
67 * Validates the input.
69 * @throws \RuntimeException When not enough arguments are given
71 public function validate()
73 if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
74 throw new \RuntimeException('Not enough arguments.');
79 * Checks if the input is interactive.
81 * @return Boolean Returns true if the input is interactive
83 public function isInteractive()
85 return $this->interactive;
89 * Sets the input interactivity.
91 * @param Boolean $interactive If the input should be interactive
93 public function setInteractive($interactive)
95 $this->interactive = (Boolean) $interactive;
99 * Returns the argument values.
101 * @return array An array of argument values
103 public function getArguments()
105 return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
109 * Returns the argument value for a given argument name.
111 * @param string $name The argument name
113 * @return mixed The argument value
115 * @throws \InvalidArgumentException When argument given doesn't exist
117 public function getArgument($name)
119 if (!$this->definition->hasArgument($name)) {
120 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
123 return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
127 * Sets an argument value by name.
129 * @param string $name The argument name
130 * @param string $value The argument value
132 * @throws \InvalidArgumentException When argument given doesn't exist
134 public function setArgument($name, $value)
136 if (!$this->definition->hasArgument($name)) {
137 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
140 $this->arguments[$name] = $value;
144 * Returns true if an InputArgument object exists by name or position.
146 * @param string|integer $name The InputArgument name or position
148 * @return Boolean true if the InputArgument object exists, false otherwise
150 public function hasArgument($name)
152 return $this->definition->hasArgument($name);
156 * Returns the options values.
158 * @return array An array of option values
160 public function getOptions()
162 return array_merge($this->definition->getOptionDefaults(), $this->options);
166 * Returns the option value for a given option name.
168 * @param string $name The option name
170 * @return mixed The option value
172 * @throws \InvalidArgumentException When option given doesn't exist
174 public function getOption($name)
176 if (!$this->definition->hasOption($name)) {
177 throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
180 return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
184 * Sets an option value by name.
186 * @param string $name The option name
187 * @param string $value The option value
189 * @throws \InvalidArgumentException When option given doesn't exist
191 public function setOption($name, $value)
193 if (!$this->definition->hasOption($name)) {
194 throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
197 $this->options[$name] = $value;
201 * Returns true if an InputOption object exists by name.
203 * @param string $name The InputOption name
205 * @return Boolean true if the InputOption object exists, false otherwise
207 public function hasOption($name)
209 return $this->definition->hasOption($name);