InputOption.php 5.9 KB

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