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 * Represents a command line argument.
20 * @author Fabien Potencier <fabien@symfony.com>
34 * @param string $name The argument name
35 * @param int $mode The argument mode: self::REQUIRED or self::OPTIONAL
36 * @param string $description A description text
37 * @param mixed $default The default value (for self::OPTIONAL mode only)
39 * @throws InvalidArgumentException When argument mode is not valid
41 public function __construct($name, $mode = null, $description = '', $default = null)
44 $mode = self::OPTIONAL;
45 } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
46 throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
51 $this->description = $description;
53 $this->setDefault($default);
57 * Returns the argument name.
59 * @return string The argument name
61 public function getName()
67 * Returns true if the argument is required.
69 * @return bool true if parameter mode is self::REQUIRED, false otherwise
71 public function isRequired()
73 return self::REQUIRED === (self::REQUIRED & $this->mode);
77 * Returns true if the argument can take multiple values.
79 * @return bool true if mode is self::IS_ARRAY, false otherwise
81 public function isArray()
83 return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
87 * Sets the default value.
89 * @param mixed $default The default value
91 * @throws LogicException When incorrect default value is given
93 public function setDefault($default = null)
95 if (self::REQUIRED === $this->mode && null !== $default) {
96 throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
99 if ($this->isArray()) {
100 if (null === $default) {
102 } elseif (!is_array($default)) {
103 throw new LogicException('A default value for an array argument must be an array.');
107 $this->default = $default;
111 * Returns the default value.
113 * @return mixed The default value
115 public function getDefault()
117 return $this->default;
121 * Returns the description text.
123 * @return string The description text
125 public function getDescription()
127 return $this->description;