ArgvInput.php
1 year ago
ArrayInput.php
1 year ago
Input.php
1 year ago
InputArgument.php
2 years ago
InputAwareInterface.php
2 years ago
InputDefinition.php
1 year ago
InputInterface.php
1 year ago
InputOption.php
1 year ago
StreamableInputInterface.php
2 years ago
StringInput.php
2 years ago
InputOption.php
196 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\Console\Input; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\Console\Exception\InvalidArgumentException; |
| 14 | use Matomo\Dependencies\Symfony\Component\Console\Exception\LogicException; |
| 15 | /** |
| 16 | * Represents a command line option. |
| 17 | * |
| 18 | * @author Fabien Potencier <fabien@symfony.com> |
| 19 | */ |
| 20 | class InputOption |
| 21 | { |
| 22 | /** |
| 23 | * Do not accept input for the option (e.g. --yell). This is the default behavior of options. |
| 24 | */ |
| 25 | public const VALUE_NONE = 1; |
| 26 | /** |
| 27 | * A value must be passed when the option is used (e.g. --iterations=5 or -i5). |
| 28 | */ |
| 29 | public const VALUE_REQUIRED = 2; |
| 30 | /** |
| 31 | * The option may or may not have a value (e.g. --yell or --yell=loud). |
| 32 | */ |
| 33 | public const VALUE_OPTIONAL = 4; |
| 34 | /** |
| 35 | * The option accepts multiple values (e.g. --dir=/foo --dir=/bar). |
| 36 | */ |
| 37 | public const VALUE_IS_ARRAY = 8; |
| 38 | /** |
| 39 | * The option may have either positive or negative value (e.g. --ansi or --no-ansi). |
| 40 | */ |
| 41 | public const VALUE_NEGATABLE = 16; |
| 42 | private $name; |
| 43 | private $shortcut; |
| 44 | private $mode; |
| 45 | private $default; |
| 46 | private $description; |
| 47 | /** |
| 48 | * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
| 49 | * @param int|null $mode The option mode: One of the VALUE_* constants |
| 50 | * @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE) |
| 51 | * |
| 52 | * @throws InvalidArgumentException If option mode is invalid or incompatible |
| 53 | */ |
| 54 | public function __construct(string $name, $shortcut = null, ?int $mode = null, string $description = '', $default = null) |
| 55 | { |
| 56 | if (str_starts_with($name, '--')) { |
| 57 | $name = substr($name, 2); |
| 58 | } |
| 59 | if (empty($name)) { |
| 60 | throw new InvalidArgumentException('An option name cannot be empty.'); |
| 61 | } |
| 62 | if ('' === $shortcut || [] === $shortcut || \false === $shortcut) { |
| 63 | $shortcut = null; |
| 64 | } |
| 65 | if (null !== $shortcut) { |
| 66 | if (\is_array($shortcut)) { |
| 67 | $shortcut = implode('|', $shortcut); |
| 68 | } |
| 69 | $shortcuts = preg_split('{(\\|)-?}', ltrim($shortcut, '-')); |
| 70 | $shortcuts = array_filter($shortcuts, 'strlen'); |
| 71 | $shortcut = implode('|', $shortcuts); |
| 72 | if ('' === $shortcut) { |
| 73 | throw new InvalidArgumentException('An option shortcut cannot be empty.'); |
| 74 | } |
| 75 | } |
| 76 | if (null === $mode) { |
| 77 | $mode = self::VALUE_NONE; |
| 78 | } elseif ($mode >= self::VALUE_NEGATABLE << 1 || $mode < 1) { |
| 79 | throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); |
| 80 | } |
| 81 | $this->name = $name; |
| 82 | $this->shortcut = $shortcut; |
| 83 | $this->mode = $mode; |
| 84 | $this->description = $description; |
| 85 | if ($this->isArray() && !$this->acceptValue()) { |
| 86 | throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.'); |
| 87 | } |
| 88 | if ($this->isNegatable() && $this->acceptValue()) { |
| 89 | throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.'); |
| 90 | } |
| 91 | $this->setDefault($default); |
| 92 | } |
| 93 | /** |
| 94 | * Returns the option shortcut. |
| 95 | * |
| 96 | * @return string|null |
| 97 | */ |
| 98 | public function getShortcut() |
| 99 | { |
| 100 | return $this->shortcut; |
| 101 | } |
| 102 | /** |
| 103 | * Returns the option name. |
| 104 | * |
| 105 | * @return string |
| 106 | */ |
| 107 | public function getName() |
| 108 | { |
| 109 | return $this->name; |
| 110 | } |
| 111 | /** |
| 112 | * Returns true if the option accepts a value. |
| 113 | * |
| 114 | * @return bool true if value mode is not self::VALUE_NONE, false otherwise |
| 115 | */ |
| 116 | public function acceptValue() |
| 117 | { |
| 118 | return $this->isValueRequired() || $this->isValueOptional(); |
| 119 | } |
| 120 | /** |
| 121 | * Returns true if the option requires a value. |
| 122 | * |
| 123 | * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise |
| 124 | */ |
| 125 | public function isValueRequired() |
| 126 | { |
| 127 | return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); |
| 128 | } |
| 129 | /** |
| 130 | * Returns true if the option takes an optional value. |
| 131 | * |
| 132 | * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise |
| 133 | */ |
| 134 | public function isValueOptional() |
| 135 | { |
| 136 | return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); |
| 137 | } |
| 138 | /** |
| 139 | * Returns true if the option can take multiple values. |
| 140 | * |
| 141 | * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise |
| 142 | */ |
| 143 | public function isArray() |
| 144 | { |
| 145 | return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); |
| 146 | } |
| 147 | public function isNegatable() : bool |
| 148 | { |
| 149 | return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode); |
| 150 | } |
| 151 | /** |
| 152 | * @param string|bool|int|float|array|null $default |
| 153 | */ |
| 154 | public function setDefault($default = null) |
| 155 | { |
| 156 | if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { |
| 157 | throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); |
| 158 | } |
| 159 | if ($this->isArray()) { |
| 160 | if (null === $default) { |
| 161 | $default = []; |
| 162 | } elseif (!\is_array($default)) { |
| 163 | throw new LogicException('A default value for an array option must be an array.'); |
| 164 | } |
| 165 | } |
| 166 | $this->default = $this->acceptValue() || $this->isNegatable() ? $default : \false; |
| 167 | } |
| 168 | /** |
| 169 | * Returns the default value. |
| 170 | * |
| 171 | * @return string|bool|int|float|array|null |
| 172 | */ |
| 173 | public function getDefault() |
| 174 | { |
| 175 | return $this->default; |
| 176 | } |
| 177 | /** |
| 178 | * Returns the description text. |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public function getDescription() |
| 183 | { |
| 184 | return $this->description; |
| 185 | } |
| 186 | /** |
| 187 | * Checks whether the given option equals this one. |
| 188 | * |
| 189 | * @return bool |
| 190 | */ |
| 191 | public function equals(self $option) |
| 192 | { |
| 193 | return $option->getName() === $this->getName() && $option->getShortcut() === $this->getShortcut() && $option->getDefault() === $this->getDefault() && $option->isNegatable() === $this->isNegatable() && $option->isArray() === $this->isArray() && $option->isValueRequired() === $this->isValueRequired() && $option->isValueOptional() === $this->isValueOptional(); |
| 194 | } |
| 195 | } |
| 196 |