InputOption.php 6.6 KB

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