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
31 * @var InputDefinition
33 protected $definition;
34 protected $options = array();
35 protected $arguments = array();
36 protected $interactive = true;
41 * @param InputDefinition|null $definition A InputDefinition instance
43 public function __construct(InputDefinition $definition = null)
45 if (null === $definition) {
46 $this->definition = new InputDefinition();
48 $this->bind($definition);
56 public function bind(InputDefinition $definition)
58 $this->arguments = array();
59 $this->options = array();
60 $this->definition = $definition;
66 * Processes command line arguments.
68 abstract protected function parse();
73 public function validate()
75 $definition = $this->definition;
76 $givenArguments = $this->arguments;
78 $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
79 return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
82 if (count($missingArguments) > 0) {
83 throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
90 public function isInteractive()
92 return $this->interactive;
98 public function setInteractive($interactive)
100 $this->interactive = (bool) $interactive;
106 public function getArguments()
108 return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
114 public function getArgument($name)
116 if (!$this->definition->hasArgument($name)) {
117 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
120 return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
126 public function setArgument($name, $value)
128 if (!$this->definition->hasArgument($name)) {
129 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
132 $this->arguments[$name] = $value;
138 public function hasArgument($name)
140 return $this->definition->hasArgument($name);
146 public function getOptions()
148 return array_merge($this->definition->getOptionDefaults(), $this->options);
154 public function getOption($name)
156 if (!$this->definition->hasOption($name)) {
157 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
160 return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
166 public function setOption($name, $value)
168 if (!$this->definition->hasOption($name)) {
169 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
172 $this->options[$name] = $value;
178 public function hasOption($name)
180 return $this->definition->hasOption($name);
184 * Escapes a token through escapeshellarg if it contains unsafe chars.
186 * @param string $token
190 public function escapeToken($token)
192 return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);