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;
15 * Represents a command line argument.
17 * @author Fabien Potencier <fabien@symfony.com>
35 * @param string $name The argument name
36 * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
37 * @param string $description A description text
38 * @param mixed $default The default value (for self::OPTIONAL mode only)
40 * @throws \InvalidArgumentException When argument mode is not valid
44 public function __construct($name, $mode = null, $description = '', $default = null)
47 $mode = self::OPTIONAL;
48 } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
49 throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
54 $this->description = $description;
56 $this->setDefault($default);
60 * Returns the argument name.
62 * @return string The argument name
64 public function getName()
70 * Returns true if the argument is required.
72 * @return Boolean true if parameter mode is self::REQUIRED, false otherwise
74 public function isRequired()
76 return self::REQUIRED === (self::REQUIRED & $this->mode);
80 * Returns true if the argument can take multiple values.
82 * @return Boolean true if mode is self::IS_ARRAY, false otherwise
84 public function isArray()
86 return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
90 * Sets the default value.
92 * @param mixed $default The default value
94 * @throws \LogicException When incorrect default value is given
96 public function setDefault($default = null)
98 if (self::REQUIRED === $this->mode && null !== $default) {
99 throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.');
102 if ($this->isArray()) {
103 if (null === $default) {
105 } elseif (!is_array($default)) {
106 throw new \LogicException('A default value for an array argument must be an array.');
110 $this->default = $default;
114 * Returns the default value.
116 * @return mixed The default value
118 public function getDefault()
120 return $this->default;
124 * Returns the description text.
126 * @return string The description text
128 public function getDescription()
130 return $this->description;