InputOption.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 $name;
  41. private $shortcut;
  42. private $mode;
  43. private $default;
  44. private $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, $shortcut = null, int $mode = null, string $description = '', $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. * @return string|null
  95. */
  96. public function getShortcut()
  97. {
  98. return $this->shortcut;
  99. }
  100. /**
  101. * Returns the option name.
  102. *
  103. * @return string
  104. */
  105. public function getName()
  106. {
  107. return $this->name;
  108. }
  109. /**
  110. * Returns true if the option accepts a value.
  111. *
  112. * @return bool true if value mode is not self::VALUE_NONE, false otherwise
  113. */
  114. public function acceptValue()
  115. {
  116. return $this->isValueRequired() || $this->isValueOptional();
  117. }
  118. /**
  119. * Returns true if the option requires a value.
  120. *
  121. * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
  122. */
  123. public function isValueRequired()
  124. {
  125. return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
  126. }
  127. /**
  128. * Returns true if the option takes an optional value.
  129. *
  130. * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
  131. */
  132. public function isValueOptional()
  133. {
  134. return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
  135. }
  136. /**
  137. * Returns true if the option can take multiple values.
  138. *
  139. * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
  140. */
  141. public function isArray()
  142. {
  143. return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
  144. }
  145. public function isNegatable(): bool
  146. {
  147. return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode);
  148. }
  149. /**
  150. * @param string|bool|int|float|array|null $default
  151. */
  152. public function setDefault($default = null)
  153. {
  154. if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
  155. throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
  156. }
  157. if ($this->isArray()) {
  158. if (null === $default) {
  159. $default = [];
  160. } elseif (!\is_array($default)) {
  161. throw new LogicException('A default value for an array option must be an array.');
  162. }
  163. }
  164. $this->default = $this->acceptValue() || $this->isNegatable() ? $default : false;
  165. }
  166. /**
  167. * Returns the default value.
  168. *
  169. * @return string|bool|int|float|array|null
  170. */
  171. public function getDefault()
  172. {
  173. return $this->default;
  174. }
  175. /**
  176. * Returns the description text.
  177. *
  178. * @return string
  179. */
  180. public function getDescription()
  181. {
  182. return $this->description;
  183. }
  184. /**
  185. * Checks whether the given option equals this one.
  186. *
  187. * @return bool
  188. */
  189. public function equals(self $option)
  190. {
  191. return $option->getName() === $this->getName()
  192. && $option->getShortcut() === $this->getShortcut()
  193. && $option->getDefault() === $this->getDefault()
  194. && $option->isNegatable() === $this->isNegatable()
  195. && $option->isArray() === $this->isArray()
  196. && $option->isValueRequired() === $this->isValueRequired()
  197. && $option->isValueOptional() === $this->isValueOptional()
  198. ;
  199. }
  200. }