InputOption.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Symfony\Component\Console\Input;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Represents a command line option.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class InputOption
  17. {
  18. const VALUE_NONE = 1;
  19. const VALUE_REQUIRED = 2;
  20. const VALUE_OPTIONAL = 4;
  21. const VALUE_IS_ARRAY = 8;
  22. protected $name;
  23. protected $shortcut;
  24. protected $mode;
  25. protected $default;
  26. protected $description;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $name The option name
  31. * @param string $shortcut The shortcut (can be null)
  32. * @param integer $mode The option mode: One of the VALUE_* constants
  33. * @param string $description A description text
  34. * @param mixed $default The default value (must be null for self::VALUE_REQUIRED or self::VALUE_NONE)
  35. *
  36. * @throws \InvalidArgumentException If option mode is invalid or incompatible
  37. */
  38. public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
  39. {
  40. if ('--' === substr($name, 0, 2)) {
  41. $name = substr($name, 2);
  42. }
  43. if (empty($shortcut)) {
  44. $shortcut = null;
  45. }
  46. if (null !== $shortcut) {
  47. if ('-' === $shortcut[0]) {
  48. $shortcut = substr($shortcut, 1);
  49. }
  50. }
  51. if (null === $mode) {
  52. $mode = self::VALUE_NONE;
  53. } else if (!is_int($mode) || $mode > 15) {
  54. throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
  55. }
  56. $this->name = $name;
  57. $this->shortcut = $shortcut;
  58. $this->mode = $mode;
  59. $this->description = $description;
  60. if ($this->isArray() && !$this->acceptValue()) {
  61. throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
  62. }
  63. $this->setDefault($default);
  64. }
  65. /**
  66. * Returns the shortcut.
  67. *
  68. * @return string The shortcut
  69. */
  70. public function getShortcut()
  71. {
  72. return $this->shortcut;
  73. }
  74. /**
  75. * Returns the name.
  76. *
  77. * @return string The name
  78. */
  79. public function getName()
  80. {
  81. return $this->name;
  82. }
  83. /**
  84. * Returns true if the option accepts a value.
  85. *
  86. * @return Boolean true if value mode is not self::VALUE_NONE, false otherwise
  87. */
  88. public function acceptValue()
  89. {
  90. return $this->isValueRequired() || $this->isValueOptional();
  91. }
  92. /**
  93. * Returns true if the option requires a value.
  94. *
  95. * @return Boolean true if value mode is self::VALUE_REQUIRED, false otherwise
  96. */
  97. public function isValueRequired()
  98. {
  99. return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
  100. }
  101. /**
  102. * Returns true if the option takes an optional value.
  103. *
  104. * @return Boolean true if value mode is self::VALUE_OPTIONAL, false otherwise
  105. */
  106. public function isValueOptional()
  107. {
  108. return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
  109. }
  110. /**
  111. * Returns true if the option can take multiple values.
  112. *
  113. * @return Boolean true if mode is self::VALUE_IS_ARRAY, false otherwise
  114. */
  115. public function isArray()
  116. {
  117. return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
  118. }
  119. /**
  120. * Sets the default value.
  121. *
  122. * @param mixed $default The default value
  123. */
  124. public function setDefault($default = null)
  125. {
  126. if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
  127. throw new \LogicException('Cannot set a default value when using Option::VALUE_NONE mode.');
  128. }
  129. if ($this->isArray()) {
  130. if (null === $default) {
  131. $default = array();
  132. } elseif (!is_array($default)) {
  133. throw new \LogicException('A default value for an array option must be an array.');
  134. }
  135. }
  136. $this->default = $this->acceptValue() ? $default : false;
  137. }
  138. /**
  139. * Returns the default value.
  140. *
  141. * @return mixed The default value
  142. */
  143. public function getDefault()
  144. {
  145. return $this->default;
  146. }
  147. /**
  148. * Returns the description text.
  149. *
  150. * @return string The description text
  151. */
  152. public function getDescription()
  153. {
  154. return $this->description;
  155. }
  156. }