123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace Symfony\Component\Console\Input;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Completion\CompletionInput;
- use Symfony\Component\Console\Completion\CompletionSuggestions;
- use Symfony\Component\Console\Completion\Suggestion;
- use Symfony\Component\Console\Exception\InvalidArgumentException;
- use Symfony\Component\Console\Exception\LogicException;
- class InputOption
- {
-
- public const VALUE_NONE = 1;
-
- public const VALUE_REQUIRED = 2;
-
- public const VALUE_OPTIONAL = 4;
-
- public const VALUE_IS_ARRAY = 8;
-
- public const VALUE_NEGATABLE = 16;
- private string $name;
- private string|array|null $shortcut;
- private int $mode;
- private string|int|bool|array|null|float $default;
- private array|\Closure $suggestedValues;
- private string $description;
-
- public function __construct(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', string|bool|int|float|array|null $default = null, array|\Closure $suggestedValues = [])
- {
- if (str_starts_with($name, '--')) {
- $name = substr($name, 2);
- }
- if (empty($name)) {
- throw new InvalidArgumentException('An option name cannot be empty.');
- }
- if ('' === $shortcut || [] === $shortcut || false === $shortcut) {
- $shortcut = null;
- }
- if (null !== $shortcut) {
- if (\is_array($shortcut)) {
- $shortcut = implode('|', $shortcut);
- }
- $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
- $shortcuts = array_filter($shortcuts, 'strlen');
- $shortcut = implode('|', $shortcuts);
- if ('' === $shortcut) {
- throw new InvalidArgumentException('An option shortcut cannot be empty.');
- }
- }
- if (null === $mode) {
- $mode = self::VALUE_NONE;
- } elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) {
- throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
- }
- $this->name = $name;
- $this->shortcut = $shortcut;
- $this->mode = $mode;
- $this->description = $description;
- $this->suggestedValues = $suggestedValues;
- if ($suggestedValues && !$this->acceptValue()) {
- throw new LogicException('Cannot set suggested values if the option does not accept a value.');
- }
- if ($this->isArray() && !$this->acceptValue()) {
- throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
- }
- if ($this->isNegatable() && $this->acceptValue()) {
- throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.');
- }
- $this->setDefault($default);
- }
-
- public function getShortcut(): ?string
- {
- return $this->shortcut;
- }
-
- public function getName(): string
- {
- return $this->name;
- }
-
- public function acceptValue(): bool
- {
- return $this->isValueRequired() || $this->isValueOptional();
- }
-
- public function isValueRequired(): bool
- {
- return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
- }
-
- public function isValueOptional(): bool
- {
- return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
- }
-
- public function isArray(): bool
- {
- return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
- }
- public function isNegatable(): bool
- {
- return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode);
- }
-
- public function setDefault(string|bool|int|float|array|null $default = null)
- {
- if (1 > \func_num_args()) {
- trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
- }
- if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
- throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
- }
- if ($this->isArray()) {
- if (null === $default) {
- $default = [];
- } elseif (!\is_array($default)) {
- throw new LogicException('A default value for an array option must be an array.');
- }
- }
- $this->default = $this->acceptValue() || $this->isNegatable() ? $default : false;
- }
-
- public function getDefault(): string|bool|int|float|array|null
- {
- return $this->default;
- }
-
- public function getDescription(): string
- {
- return $this->description;
- }
- public function hasCompletion(): bool
- {
- return [] !== $this->suggestedValues;
- }
-
- public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
- {
- $values = $this->suggestedValues;
- if ($values instanceof \Closure && !\is_array($values = $values($input))) {
- throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
- }
- if ($values) {
- $suggestions->suggestValues($values);
- }
- }
-
- public function equals(self $option): bool
- {
- 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()
- ;
- }
- }
|