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>
36 * @param string $name The argument name
37 * @param int $mode The argument mode: self::REQUIRED or self::OPTIONAL
38 * @param string $description A description text
39 * @param mixed $default The default value (for self::OPTIONAL mode only)
41 * @throws InvalidArgumentException When argument mode is not valid
43 public function __construct($name, $mode = null, $description = '', $default = null)
46 $mode = self::OPTIONAL;
47 } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
48 throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
53 $this->description = $description;
55 $this->setDefault($default);
59 * Returns the argument name.
61 * @return string The argument name
63 public function getName()
69 * Returns true if the argument is required.
71 * @return bool true if parameter mode is self::REQUIRED, false otherwise
73 public function isRequired()
75 return self::REQUIRED === (self::REQUIRED & $this->mode);
79 * Returns true if the argument can take multiple values.
81 * @return bool true if mode is self::IS_ARRAY, false otherwise
83 public function isArray()
85 return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
89 * Sets the default value.
91 * @param mixed $default The default value
93 * @throws LogicException When incorrect default value is given
95 public function setDefault($default = null)
97 if (self::REQUIRED === $this->mode && null !== $default) {
98 throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
101 if ($this->isArray()) {
102 if (null === $default) {
104 } elseif (!is_array($default)) {
105 throw new LogicException('A default value for an array argument must be an array.');
109 $this->default = $default;
113 * Returns the default value.
115 * @return mixed The default value
117 public function getDefault()
119 return $this->default;
123 * Returns the description text.
125 * @return string The description text
127 public function getDescription()
129 return $this->description;