You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.9 KiB

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