InputOption.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 may have either positive or negative value (e.g. --ansi or --no-ansi).
  42. */
  43. public const VALUE_NEGATABLE = 16;
  44. private string $name;
  45. private string|array|null $shortcut;
  46. private int $mode;
  47. private string|int|bool|array|null|float $default;
  48. private array|\Closure $suggestedValues;
  49. private string $description;
  50. /**
  51. * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
  52. * @param int|null $mode The option mode: One of the VALUE_* constants
  53. * @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
  54. * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
  55. *
  56. * @throws InvalidArgumentException If option mode is invalid or incompatible
  57. */
  58. 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 = [])
  59. {
  60. if (str_starts_with($name, '--')) {
  61. $name = substr($name, 2);
  62. }
  63. if (empty($name)) {
  64. throw new InvalidArgumentException('An option name cannot be empty.');
  65. }
  66. if ('' === $shortcut || [] === $shortcut || false === $shortcut) {
  67. $shortcut = null;
  68. }
  69. if (null !== $shortcut) {
  70. if (\is_array($shortcut)) {
  71. $shortcut = implode('|', $shortcut);
  72. }
  73. $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
  74. $shortcuts = array_filter($shortcuts, 'strlen');
  75. $shortcut = implode('|', $shortcuts);
  76. if ('' === $shortcut) {
  77. throw new InvalidArgumentException('An option shortcut cannot be empty.');
  78. }
  79. }
  80. if (null === $mode) {
  81. $mode = self::VALUE_NONE;
  82. } elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) {
  83. throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
  84. }
  85. $this->name = $name;
  86. $this->shortcut = $shortcut;
  87. $this->mode = $mode;
  88. $this->description = $description;
  89. $this->suggestedValues = $suggestedValues;
  90. if ($suggestedValues && !$this->acceptValue()) {
  91. throw new LogicException('Cannot set suggested values if the option does not accept a value.');
  92. }
  93. if ($this->isArray() && !$this->acceptValue()) {
  94. throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
  95. }
  96. if ($this->isNegatable() && $this->acceptValue()) {
  97. throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.');
  98. }
  99. $this->setDefault($default);
  100. }
  101. /**
  102. * Returns the option shortcut.
  103. */
  104. public function getShortcut(): ?string
  105. {
  106. return $this->shortcut;
  107. }
  108. /**
  109. * Returns the option name.
  110. */
  111. public function getName(): string
  112. {
  113. return $this->name;
  114. }
  115. /**
  116. * Returns true if the option accepts a value.
  117. *
  118. * @return bool true if value mode is not self::VALUE_NONE, false otherwise
  119. */
  120. public function acceptValue(): bool
  121. {
  122. return $this->isValueRequired() || $this->isValueOptional();
  123. }
  124. /**
  125. * Returns true if the option requires a value.
  126. *
  127. * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
  128. */
  129. public function isValueRequired(): bool
  130. {
  131. return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
  132. }
  133. /**
  134. * Returns true if the option takes an optional value.
  135. *
  136. * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
  137. */
  138. public function isValueOptional(): bool
  139. {
  140. return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
  141. }
  142. /**
  143. * Returns true if the option can take multiple values.
  144. *
  145. * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
  146. */
  147. public function isArray(): bool
  148. {
  149. return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
  150. }
  151. public function isNegatable(): bool
  152. {
  153. return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode);
  154. }
  155. /**
  156. * @return void
  157. */
  158. public function setDefault(string|bool|int|float|array|null $default = null)
  159. {
  160. if (1 > \func_num_args()) {
  161. trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  162. }
  163. if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
  164. throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
  165. }
  166. if ($this->isArray()) {
  167. if (null === $default) {
  168. $default = [];
  169. } elseif (!\is_array($default)) {
  170. throw new LogicException('A default value for an array option must be an array.');
  171. }
  172. }
  173. $this->default = $this->acceptValue() || $this->isNegatable() ? $default : false;
  174. }
  175. /**
  176. * Returns the default value.
  177. */
  178. public function getDefault(): string|bool|int|float|array|null
  179. {
  180. return $this->default;
  181. }
  182. /**
  183. * Returns the description text.
  184. */
  185. public function getDescription(): string
  186. {
  187. return $this->description;
  188. }
  189. public function hasCompletion(): bool
  190. {
  191. return [] !== $this->suggestedValues;
  192. }
  193. /**
  194. * Adds suggestions to $suggestions for the current completion input.
  195. *
  196. * @see Command::complete()
  197. */
  198. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  199. {
  200. $values = $this->suggestedValues;
  201. if ($values instanceof \Closure && !\is_array($values = $values($input))) {
  202. throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
  203. }
  204. if ($values) {
  205. $suggestions->suggestValues($values);
  206. }
  207. }
  208. /**
  209. * Checks whether the given option equals this one.
  210. */
  211. public function equals(self $option): bool
  212. {
  213. return $option->getName() === $this->getName()
  214. && $option->getShortcut() === $this->getShortcut()
  215. && $option->getDefault() === $this->getDefault()
  216. && $option->isNegatable() === $this->isNegatable()
  217. && $option->isArray() === $this->isArray()
  218. && $option->isValueRequired() === $this->isValueRequired()
  219. && $option->isValueOptional() === $this->isValueOptional()
  220. ;
  221. }
  222. }