InputOption.php 8.5 KB

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