InputOption.php 5.8 KB

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