InputOption.php 5.3 KB

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