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 option.
20 * @author Fabien Potencier <fabien@symfony.com>
25 const VALUE_REQUIRED = 2;
26 const VALUE_OPTIONAL = 4;
27 const VALUE_IS_ARRAY = 8;
36 * @param string $name The option name
37 * @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
38 * @param int $mode The option mode: One of the VALUE_* constants
39 * @param string $description A description text
40 * @param mixed $default The default value (must be null for self::VALUE_NONE)
42 * @throws InvalidArgumentException If option mode is invalid or incompatible
44 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
46 if (0 === strpos($name, '--')) {
47 $name = substr($name, 2);
51 throw new InvalidArgumentException('An option name cannot be empty.');
54 if (empty($shortcut)) {
58 if (null !== $shortcut) {
59 if (is_array($shortcut)) {
60 $shortcut = implode('|', $shortcut);
62 $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
63 $shortcuts = array_filter($shortcuts);
64 $shortcut = implode('|', $shortcuts);
66 if (empty($shortcut)) {
67 throw new InvalidArgumentException('An option shortcut cannot be empty.');
72 $mode = self::VALUE_NONE;
73 } elseif (!is_int($mode) || $mode > 15 || $mode < 1) {
74 throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
78 $this->shortcut = $shortcut;
80 $this->description = $description;
82 if ($this->isArray() && !$this->acceptValue()) {
83 throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
86 $this->setDefault($default);
90 * Returns the option shortcut.
92 * @return string The shortcut
94 public function getShortcut()
96 return $this->shortcut;
100 * Returns the option name.
102 * @return string The name
104 public function getName()
110 * Returns true if the option accepts a value.
112 * @return bool true if value mode is not self::VALUE_NONE, false otherwise
114 public function acceptValue()
116 return $this->isValueRequired() || $this->isValueOptional();
120 * Returns true if the option requires a value.
122 * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
124 public function isValueRequired()
126 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
130 * Returns true if the option takes an optional value.
132 * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
134 public function isValueOptional()
136 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
140 * Returns true if the option can take multiple values.
142 * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
144 public function isArray()
146 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
150 * Sets the default value.
152 * @param mixed $default The default value
154 * @throws LogicException When incorrect default value is given
156 public function setDefault($default = null)
158 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
159 throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
162 if ($this->isArray()) {
163 if (null === $default) {
165 } elseif (!is_array($default)) {
166 throw new LogicException('A default value for an array option must be an array.');
170 $this->default = $this->acceptValue() ? $default : false;
174 * Returns the default value.
176 * @return mixed The default value
178 public function getDefault()
180 return $this->default;
184 * Returns the description text.
186 * @return string The description text
188 public function getDescription()
190 return $this->description;
194 * Checks whether the given option equals this one.
198 public function equals(self $option)
200 return $option->getName() === $this->getName()
201 && $option->getShortcut() === $this->getShortcut()
202 && $option->getDefault() === $this->getDefault()
203 && $option->isArray() === $this->isArray()
204 && $option->isValueRequired() === $this->isValueRequired()
205 && $option->isValueOptional() === $this->isValueOptional()