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
30 protected $definition;
32 protected $options = array();
33 protected $arguments = array();
34 protected $interactive = true;
36 public function __construct(InputDefinition $definition = null)
38 if (null === $definition) {
39 $this->definition = new InputDefinition();
41 $this->bind($definition);
49 public function bind(InputDefinition $definition)
51 $this->arguments = array();
52 $this->options = array();
53 $this->definition = $definition;
59 * Processes command line arguments.
61 abstract protected function parse();
66 public function validate()
68 $definition = $this->definition;
69 $givenArguments = $this->arguments;
71 $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
72 return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
75 if (\count($missingArguments) > 0) {
76 throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
83 public function isInteractive()
85 return $this->interactive;
91 public function setInteractive($interactive)
93 $this->interactive = (bool) $interactive;
99 public function getArguments()
101 return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
107 public function getArgument($name)
109 if (!$this->definition->hasArgument($name)) {
110 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
113 return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
119 public function setArgument($name, $value)
121 if (!$this->definition->hasArgument($name)) {
122 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
125 $this->arguments[$name] = $value;
131 public function hasArgument($name)
133 return $this->definition->hasArgument($name);
139 public function getOptions()
141 return array_merge($this->definition->getOptionDefaults(), $this->options);
147 public function getOption($name)
149 if (!$this->definition->hasOption($name)) {
150 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
153 return array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
159 public function setOption($name, $value)
161 if (!$this->definition->hasOption($name)) {
162 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
165 $this->options[$name] = $value;
171 public function hasOption($name)
173 return $this->definition->hasOption($name);
177 * Escapes a token through escapeshellarg if it contains unsafe chars.
179 * @param string $token
183 public function escapeToken($token)
185 return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
191 public function setStream($stream)
193 $this->stream = $stream;
199 public function getStream()
201 return $this->stream;