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;
38 * @param string $name The option name
39 * @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
40 * @param int $mode The option mode: One of the VALUE_* constants
41 * @param string $description A description text
42 * @param mixed $default The default value (must be null for self::VALUE_NONE)
44 * @throws InvalidArgumentException If option mode is invalid or incompatible
46 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
48 if (0 === strpos($name, '--')) {
49 $name = substr($name, 2);
53 throw new InvalidArgumentException('An option name cannot be empty.');
56 if (empty($shortcut)) {
60 if (null !== $shortcut) {
61 if (is_array($shortcut)) {
62 $shortcut = implode('|', $shortcut);
64 $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
65 $shortcuts = array_filter($shortcuts);
66 $shortcut = implode('|', $shortcuts);
68 if (empty($shortcut)) {
69 throw new InvalidArgumentException('An option shortcut cannot be empty.');
74 $mode = self::VALUE_NONE;
75 } elseif (!is_int($mode) || $mode > 15 || $mode < 1) {
76 throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
80 $this->shortcut = $shortcut;
82 $this->description = $description;
84 if ($this->isArray() && !$this->acceptValue()) {
85 throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
88 $this->setDefault($default);
92 * Returns the option shortcut.
94 * @return string The shortcut
96 public function getShortcut()
98 return $this->shortcut;
102 * Returns the option name.
104 * @return string The name
106 public function getName()
112 * Returns true if the option accepts a value.
114 * @return bool true if value mode is not self::VALUE_NONE, false otherwise
116 public function acceptValue()
118 return $this->isValueRequired() || $this->isValueOptional();
122 * Returns true if the option requires a value.
124 * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
126 public function isValueRequired()
128 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
132 * Returns true if the option takes an optional value.
134 * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
136 public function isValueOptional()
138 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
142 * Returns true if the option can take multiple values.
144 * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
146 public function isArray()
148 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
152 * Sets the default value.
154 * @param mixed $default The default value
156 * @throws LogicException When incorrect default value is given
158 public function setDefault($default = null)
160 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
161 throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
164 if ($this->isArray()) {
165 if (null === $default) {
167 } elseif (!is_array($default)) {
168 throw new LogicException('A default value for an array option must be an array.');
172 $this->default = $this->acceptValue() ? $default : false;
176 * Returns the default value.
178 * @return mixed The default value
180 public function getDefault()
182 return $this->default;
186 * Returns the description text.
188 * @return string The description text
190 public function getDescription()
192 return $this->description;
196 * Checks whether the given option equals this one.
198 * @param InputOption $option option to compare
202 public function equals(InputOption $option)
204 return $option->getName() === $this->getName()
205 && $option->getShortcut() === $this->getShortcut()
206 && $option->getDefault() === $this->getDefault()
207 && $option->isArray() === $this->isArray()
208 && $option->isValueRequired() === $this->isValueRequired()
209 && $option->isValueOptional() === $this->isValueOptional()