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\RuntimeException;
18 * Input is the base class for all concrete Input classes.
20 * Three concrete classes are provided by default:
22 * * `ArgvInput`: The input comes from the CLI arguments (argv)
23 * * `StringInput`: The input is provided as a string
24 * * `ArrayInput`: The input is provided as an array
26 * @author Fabien Potencier <fabien@symfony.com>
28 abstract class Input implements InputInterface, StreamableInputInterface
31 * @var InputDefinition
33 protected $definition;
35 protected $options = array();
36 protected $arguments = array();
37 protected $interactive = true;
42 * @param InputDefinition|null $definition A InputDefinition instance
44 public function __construct(InputDefinition $definition = null)
46 if (null === $definition) {
47 $this->definition = new InputDefinition();
49 $this->bind($definition);
57 public function bind(InputDefinition $definition)
59 $this->arguments = array();
60 $this->options = array();
61 $this->definition = $definition;
67 * Processes command line arguments.
69 abstract protected function parse();
74 public function validate()
76 $definition = $this->definition;
77 $givenArguments = $this->arguments;
79 $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
80 return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
83 if (count($missingArguments) > 0) {
84 throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
91 public function isInteractive()
93 return $this->interactive;
99 public function setInteractive($interactive)
101 $this->interactive = (bool) $interactive;
107 public function getArguments()
109 return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
115 public function getArgument($name)
117 if (!$this->definition->hasArgument($name)) {
118 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
121 return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
127 public function setArgument($name, $value)
129 if (!$this->definition->hasArgument($name)) {
130 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
133 $this->arguments[$name] = $value;
139 public function hasArgument($name)
141 return $this->definition->hasArgument($name);
147 public function getOptions()
149 return array_merge($this->definition->getOptionDefaults(), $this->options);
155 public function getOption($name)
157 if (!$this->definition->hasOption($name)) {
158 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
161 return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
167 public function setOption($name, $value)
169 if (!$this->definition->hasOption($name)) {
170 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
173 $this->options[$name] = $value;
179 public function hasOption($name)
181 return $this->definition->hasOption($name);
185 * Escapes a token through escapeshellarg if it contains unsafe chars.
187 * @param string $token
191 public function escapeToken($token)
193 return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
199 public function setStream($stream)
201 $this->stream = $stream;
207 public function getStream()
209 return $this->stream;