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\Descriptor\TextDescriptor;
15 use Symfony\Component\Console\Descriptor\XmlDescriptor;
16 use Symfony\Component\Console\Output\BufferedOutput;
17 use Symfony\Component\Console\Exception\InvalidArgumentException;
18 use Symfony\Component\Console\Exception\LogicException;
21 * A InputDefinition represents a set of valid command line arguments and options.
25 * $definition = new InputDefinition(array(
26 * new InputArgument('name', InputArgument::REQUIRED),
27 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
30 * @author Fabien Potencier <fabien@symfony.com>
35 private $requiredCount;
36 private $hasAnArrayArgument = false;
44 * @param array $definition An array of InputArgument and InputOption instance
46 public function __construct(array $definition = array())
48 $this->setDefinition($definition);
52 * Sets the definition of the input.
54 * @param array $definition The definition array
56 public function setDefinition(array $definition)
60 foreach ($definition as $item) {
61 if ($item instanceof InputOption) {
68 $this->setArguments($arguments);
69 $this->setOptions($options);
73 * Sets the InputArgument objects.
75 * @param InputArgument[] $arguments An array of InputArgument objects
77 public function setArguments($arguments = array())
79 $this->arguments = array();
80 $this->requiredCount = 0;
81 $this->hasOptional = false;
82 $this->hasAnArrayArgument = false;
83 $this->addArguments($arguments);
87 * Adds an array of InputArgument objects.
89 * @param InputArgument[] $arguments An array of InputArgument objects
91 public function addArguments($arguments = array())
93 if (null !== $arguments) {
94 foreach ($arguments as $argument) {
95 $this->addArgument($argument);
101 * Adds an InputArgument object.
103 * @param InputArgument $argument An InputArgument object
105 * @throws LogicException When incorrect argument is given
107 public function addArgument(InputArgument $argument)
109 if (isset($this->arguments[$argument->getName()])) {
110 throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
113 if ($this->hasAnArrayArgument) {
114 throw new LogicException('Cannot add an argument after an array argument.');
117 if ($argument->isRequired() && $this->hasOptional) {
118 throw new LogicException('Cannot add a required argument after an optional one.');
121 if ($argument->isArray()) {
122 $this->hasAnArrayArgument = true;
125 if ($argument->isRequired()) {
126 ++$this->requiredCount;
128 $this->hasOptional = true;
131 $this->arguments[$argument->getName()] = $argument;
135 * Returns an InputArgument by name or by position.
137 * @param string|int $name The InputArgument name or position
139 * @return InputArgument An InputArgument object
141 * @throws InvalidArgumentException When argument given doesn't exist
143 public function getArgument($name)
145 if (!$this->hasArgument($name)) {
146 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
149 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
151 return $arguments[$name];
155 * Returns true if an InputArgument object exists by name or position.
157 * @param string|int $name The InputArgument name or position
159 * @return bool true if the InputArgument object exists, false otherwise
161 public function hasArgument($name)
163 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
165 return isset($arguments[$name]);
169 * Gets the array of InputArgument objects.
171 * @return InputArgument[] An array of InputArgument objects
173 public function getArguments()
175 return $this->arguments;
179 * Returns the number of InputArguments.
181 * @return int The number of InputArguments
183 public function getArgumentCount()
185 return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
189 * Returns the number of required InputArguments.
191 * @return int The number of required InputArguments
193 public function getArgumentRequiredCount()
195 return $this->requiredCount;
199 * Gets the default values.
201 * @return array An array of default values
203 public function getArgumentDefaults()
206 foreach ($this->arguments as $argument) {
207 $values[$argument->getName()] = $argument->getDefault();
214 * Sets the InputOption objects.
216 * @param InputOption[] $options An array of InputOption objects
218 public function setOptions($options = array())
220 $this->options = array();
221 $this->shortcuts = array();
222 $this->addOptions($options);
226 * Adds an array of InputOption objects.
228 * @param InputOption[] $options An array of InputOption objects
230 public function addOptions($options = array())
232 foreach ($options as $option) {
233 $this->addOption($option);
238 * Adds an InputOption object.
240 * @param InputOption $option An InputOption object
242 * @throws LogicException When option given already exist
244 public function addOption(InputOption $option)
246 if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
247 throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
250 if ($option->getShortcut()) {
251 foreach (explode('|', $option->getShortcut()) as $shortcut) {
252 if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
253 throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
258 $this->options[$option->getName()] = $option;
259 if ($option->getShortcut()) {
260 foreach (explode('|', $option->getShortcut()) as $shortcut) {
261 $this->shortcuts[$shortcut] = $option->getName();
267 * Returns an InputOption by name.
269 * @param string $name The InputOption name
271 * @return InputOption A InputOption object
273 * @throws InvalidArgumentException When option given doesn't exist
275 public function getOption($name)
277 if (!$this->hasOption($name)) {
278 throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
281 return $this->options[$name];
285 * Returns true if an InputOption object exists by name.
287 * This method can't be used to check if the user included the option when
288 * executing the command (use getOption() instead).
290 * @param string $name The InputOption name
292 * @return bool true if the InputOption object exists, false otherwise
294 public function hasOption($name)
296 return isset($this->options[$name]);
300 * Gets the array of InputOption objects.
302 * @return InputOption[] An array of InputOption objects
304 public function getOptions()
306 return $this->options;
310 * Returns true if an InputOption object exists by shortcut.
312 * @param string $name The InputOption shortcut
314 * @return bool true if the InputOption object exists, false otherwise
316 public function hasShortcut($name)
318 return isset($this->shortcuts[$name]);
322 * Gets an InputOption by shortcut.
324 * @param string $shortcut the Shortcut name
326 * @return InputOption An InputOption object
328 public function getOptionForShortcut($shortcut)
330 return $this->getOption($this->shortcutToName($shortcut));
334 * Gets an array of default values.
336 * @return array An array of all default values
338 public function getOptionDefaults()
341 foreach ($this->options as $option) {
342 $values[$option->getName()] = $option->getDefault();
349 * Returns the InputOption name given a shortcut.
351 * @param string $shortcut The shortcut
353 * @return string The InputOption name
355 * @throws InvalidArgumentException When option given does not exist
357 private function shortcutToName($shortcut)
359 if (!isset($this->shortcuts[$shortcut])) {
360 throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
363 return $this->shortcuts[$shortcut];
369 * @param bool $short Whether to return the short version (with options folded) or not
371 * @return string The synopsis
373 public function getSynopsis($short = false)
377 if ($short && $this->getOptions()) {
378 $elements[] = '[options]';
380 foreach ($this->getOptions() as $option) {
382 if ($option->acceptValue()) {
385 $option->isValueOptional() ? '[' : '',
386 strtoupper($option->getName()),
387 $option->isValueOptional() ? ']' : ''
391 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
392 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
396 if (count($elements) && $this->getArguments()) {
397 $elements[] = '[--]';
400 foreach ($this->getArguments() as $argument) {
401 $element = '<'.$argument->getName().'>';
402 if (!$argument->isRequired()) {
403 $element = '['.$element.']';
404 } elseif ($argument->isArray()) {
405 $element = $element.' ('.$element.')';
408 if ($argument->isArray()) {
412 $elements[] = $element;
415 return implode(' ', $elements);
419 * Returns a textual representation of the InputDefinition.
421 * @return string A string representing the InputDefinition
423 * @deprecated since version 2.3, to be removed in 3.0.
425 public function asText()
427 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
429 $descriptor = new TextDescriptor();
430 $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
431 $descriptor->describe($output, $this, array('raw_output' => true));
433 return $output->fetch();
437 * Returns an XML representation of the InputDefinition.
439 * @param bool $asDom Whether to return a DOM or an XML string
441 * @return string|\DOMDocument An XML string representing the InputDefinition
443 * @deprecated since version 2.3, to be removed in 3.0.
445 public function asXml($asDom = false)
447 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
449 $descriptor = new XmlDescriptor();
452 return $descriptor->getInputDefinitionDocument($this);
455 $output = new BufferedOutput();
456 $descriptor->describe($output, $this);
458 return $output->fetch();