InputOption.php 8.3 KB

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