설명 없음

InputOption.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. private $name;
  37. private $shortcut;
  38. private $mode;
  39. private $default;
  40. private $description;
  41. /**
  42. * @param string $name The option name
  43. * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
  44. * @param int|null $mode The option mode: One of the VALUE_* constants
  45. * @param string $description A description text
  46. * @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
  47. *
  48. * @throws InvalidArgumentException If option mode is invalid or incompatible
  49. */
  50. public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
  51. {
  52. if (str_starts_with($name, '--')) {
  53. $name = substr($name, 2);
  54. }
  55. if (empty($name)) {
  56. throw new InvalidArgumentException('An option name cannot be empty.');
  57. }
  58. if (empty($shortcut)) {
  59. $shortcut = null;
  60. }
  61. if (null !== $shortcut) {
  62. if (\is_array($shortcut)) {
  63. $shortcut = implode('|', $shortcut);
  64. }
  65. $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
  66. $shortcuts = array_filter($shortcuts);
  67. $shortcut = implode('|', $shortcuts);
  68. if (empty($shortcut)) {
  69. throw new InvalidArgumentException('An option shortcut cannot be empty.');
  70. }
  71. }
  72. if (null === $mode) {
  73. $mode = self::VALUE_NONE;
  74. } elseif ($mode > 15 || $mode < 1) {
  75. throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
  76. }
  77. $this->name = $name;
  78. $this->shortcut = $shortcut;
  79. $this->mode = $mode;
  80. $this->description = $description;
  81. if ($this->isArray() && !$this->acceptValue()) {
  82. throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
  83. }
  84. $this->setDefault($default);
  85. }
  86. /**
  87. * Returns the option shortcut.
  88. *
  89. * @return string|null The shortcut
  90. */
  91. public function getShortcut()
  92. {
  93. return $this->shortcut;
  94. }
  95. /**
  96. * Returns the option name.
  97. *
  98. * @return string The name
  99. */
  100. public function getName()
  101. {
  102. return $this->name;
  103. }
  104. /**
  105. * Returns true if the option accepts a value.
  106. *
  107. * @return bool true if value mode is not self::VALUE_NONE, false otherwise
  108. */
  109. public function acceptValue()
  110. {
  111. return $this->isValueRequired() || $this->isValueOptional();
  112. }
  113. /**
  114. * Returns true if the option requires a value.
  115. *
  116. * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
  117. */
  118. public function isValueRequired()
  119. {
  120. return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
  121. }
  122. /**
  123. * Returns true if the option takes an optional value.
  124. *
  125. * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
  126. */
  127. public function isValueOptional()
  128. {
  129. return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
  130. }
  131. /**
  132. * Returns true if the option can take multiple values.
  133. *
  134. * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
  135. */
  136. public function isArray()
  137. {
  138. return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
  139. }
  140. /**
  141. * @param string|bool|int|float|array|null $default
  142. */
  143. public function setDefault($default = null)
  144. {
  145. if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
  146. throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
  147. }
  148. if ($this->isArray()) {
  149. if (null === $default) {
  150. $default = [];
  151. } elseif (!\is_array($default)) {
  152. throw new LogicException('A default value for an array option must be an array.');
  153. }
  154. }
  155. $this->default = $this->acceptValue() ? $default : false;
  156. }
  157. /**
  158. * Returns the default value.
  159. *
  160. * @return string|bool|int|float|array|null
  161. */
  162. public function getDefault()
  163. {
  164. return $this->default;
  165. }
  166. /**
  167. * Returns the description text.
  168. *
  169. * @return string The description text
  170. */
  171. public function getDescription()
  172. {
  173. return $this->description;
  174. }
  175. /**
  176. * Checks whether the given option equals this one.
  177. *
  178. * @return bool
  179. */
  180. public function equals(self $option)
  181. {
  182. return $option->getName() === $this->getName()
  183. && $option->getShortcut() === $this->getShortcut()
  184. && $option->getDefault() === $this->getDefault()
  185. && $option->isArray() === $this->isArray()
  186. && $option->isValueRequired() === $this->isValueRequired()
  187. && $option->isValueOptional() === $this->isValueOptional()
  188. ;
  189. }
  190. }