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\LogicException;
18 * A InputDefinition represents a set of valid command line arguments and options.
22 * $definition = new InputDefinition(array(
23 * new InputArgument('name', InputArgument::REQUIRED),
24 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
27 * @author Fabien Potencier <fabien@symfony.com>
32 private $requiredCount;
33 private $hasAnArrayArgument = false;
39 * @param array $definition An array of InputArgument and InputOption instance
41 public function __construct(array $definition = array())
43 $this->setDefinition($definition);
47 * Sets the definition of the input.
49 public function setDefinition(array $definition)
53 foreach ($definition as $item) {
54 if ($item instanceof InputOption) {
61 $this->setArguments($arguments);
62 $this->setOptions($options);
66 * Sets the InputArgument objects.
68 * @param InputArgument[] $arguments An array of InputArgument objects
70 public function setArguments($arguments = array())
72 $this->arguments = array();
73 $this->requiredCount = 0;
74 $this->hasOptional = false;
75 $this->hasAnArrayArgument = false;
76 $this->addArguments($arguments);
80 * Adds an array of InputArgument objects.
82 * @param InputArgument[] $arguments An array of InputArgument objects
84 public function addArguments($arguments = array())
86 if (null !== $arguments) {
87 foreach ($arguments as $argument) {
88 $this->addArgument($argument);
94 * @throws LogicException When incorrect argument is given
96 public function addArgument(InputArgument $argument)
98 if (isset($this->arguments[$argument->getName()])) {
99 throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
102 if ($this->hasAnArrayArgument) {
103 throw new LogicException('Cannot add an argument after an array argument.');
106 if ($argument->isRequired() && $this->hasOptional) {
107 throw new LogicException('Cannot add a required argument after an optional one.');
110 if ($argument->isArray()) {
111 $this->hasAnArrayArgument = true;
114 if ($argument->isRequired()) {
115 ++$this->requiredCount;
117 $this->hasOptional = true;
120 $this->arguments[$argument->getName()] = $argument;
124 * Returns an InputArgument by name or by position.
126 * @param string|int $name The InputArgument name or position
128 * @return InputArgument An InputArgument object
130 * @throws InvalidArgumentException When argument given doesn't exist
132 public function getArgument($name)
134 if (!$this->hasArgument($name)) {
135 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
138 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
140 return $arguments[$name];
144 * Returns true if an InputArgument object exists by name or position.
146 * @param string|int $name The InputArgument name or position
148 * @return bool true if the InputArgument object exists, false otherwise
150 public function hasArgument($name)
152 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
154 return isset($arguments[$name]);
158 * Gets the array of InputArgument objects.
160 * @return InputArgument[] An array of InputArgument objects
162 public function getArguments()
164 return $this->arguments;
168 * Returns the number of InputArguments.
170 * @return int The number of InputArguments
172 public function getArgumentCount()
174 return $this->hasAnArrayArgument ? PHP_INT_MAX : \count($this->arguments);
178 * Returns the number of required InputArguments.
180 * @return int The number of required InputArguments
182 public function getArgumentRequiredCount()
184 return $this->requiredCount;
188 * Gets the default values.
190 * @return array An array of default values
192 public function getArgumentDefaults()
195 foreach ($this->arguments as $argument) {
196 $values[$argument->getName()] = $argument->getDefault();
203 * Sets the InputOption objects.
205 * @param InputOption[] $options An array of InputOption objects
207 public function setOptions($options = array())
209 $this->options = array();
210 $this->shortcuts = array();
211 $this->addOptions($options);
215 * Adds an array of InputOption objects.
217 * @param InputOption[] $options An array of InputOption objects
219 public function addOptions($options = array())
221 foreach ($options as $option) {
222 $this->addOption($option);
227 * @throws LogicException When option given already exist
229 public function addOption(InputOption $option)
231 if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
232 throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
235 if ($option->getShortcut()) {
236 foreach (explode('|', $option->getShortcut()) as $shortcut) {
237 if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
238 throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
243 $this->options[$option->getName()] = $option;
244 if ($option->getShortcut()) {
245 foreach (explode('|', $option->getShortcut()) as $shortcut) {
246 $this->shortcuts[$shortcut] = $option->getName();
252 * Returns an InputOption by name.
254 * @param string $name The InputOption name
256 * @return InputOption A InputOption object
258 * @throws InvalidArgumentException When option given doesn't exist
260 public function getOption($name)
262 if (!$this->hasOption($name)) {
263 throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
266 return $this->options[$name];
270 * Returns true if an InputOption object exists by name.
272 * This method can't be used to check if the user included the option when
273 * executing the command (use getOption() instead).
275 * @param string $name The InputOption name
277 * @return bool true if the InputOption object exists, false otherwise
279 public function hasOption($name)
281 return isset($this->options[$name]);
285 * Gets the array of InputOption objects.
287 * @return InputOption[] An array of InputOption objects
289 public function getOptions()
291 return $this->options;
295 * Returns true if an InputOption object exists by shortcut.
297 * @param string $name The InputOption shortcut
299 * @return bool true if the InputOption object exists, false otherwise
301 public function hasShortcut($name)
303 return isset($this->shortcuts[$name]);
307 * Gets an InputOption by shortcut.
309 * @param string $shortcut The Shortcut name
311 * @return InputOption An InputOption object
313 public function getOptionForShortcut($shortcut)
315 return $this->getOption($this->shortcutToName($shortcut));
319 * Gets an array of default values.
321 * @return array An array of all default values
323 public function getOptionDefaults()
326 foreach ($this->options as $option) {
327 $values[$option->getName()] = $option->getDefault();
334 * Returns the InputOption name given a shortcut.
336 * @param string $shortcut The shortcut
338 * @return string The InputOption name
340 * @throws InvalidArgumentException When option given does not exist
342 private function shortcutToName($shortcut)
344 if (!isset($this->shortcuts[$shortcut])) {
345 throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
348 return $this->shortcuts[$shortcut];
354 * @param bool $short Whether to return the short version (with options folded) or not
356 * @return string The synopsis
358 public function getSynopsis($short = false)
362 if ($short && $this->getOptions()) {
363 $elements[] = '[options]';
365 foreach ($this->getOptions() as $option) {
367 if ($option->acceptValue()) {
370 $option->isValueOptional() ? '[' : '',
371 strtoupper($option->getName()),
372 $option->isValueOptional() ? ']' : ''
376 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
377 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
381 if (\count($elements) && $this->getArguments()) {
382 $elements[] = '[--]';
385 foreach ($this->getArguments() as $argument) {
386 $element = '<'.$argument->getName().'>';
387 if (!$argument->isRequired()) {
388 $element = '['.$element.']';
389 } elseif ($argument->isArray()) {
390 $element .= ' ('.$element.')';
393 if ($argument->isArray()) {
397 $elements[] = $element;
400 return implode(' ', $elements);