sfCommandOption.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Represents a command line option.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCommandOption.class.php 9076 2008-05-19 23:19:44Z Carl.Vondrick $
  16. */
  17. class sfCommandOption
  18. {
  19. const PARAMETER_NONE = 1;
  20. const PARAMETER_REQUIRED = 2;
  21. const PARAMETER_OPTIONAL = 4;
  22. const IS_ARRAY = 8;
  23. protected
  24. $name = null,
  25. $shortcut = null,
  26. $mode = null,
  27. $default = null,
  28. $help = '';
  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: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL
  35. * @param string $help A help text
  36. * @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE)
  37. */
  38. public function __construct($name, $shortcut = null, $mode = null, $help = '', $default = null)
  39. {
  40. if ('--' == substr($name, 0, 2))
  41. {
  42. $name = substr($name, 2);
  43. }
  44. if (empty($shortcut))
  45. {
  46. $shortcut = null;
  47. }
  48. if (!is_null($shortcut))
  49. {
  50. if ('-' == $shortcut[0])
  51. {
  52. $shortcut = substr($shortcut, 1);
  53. }
  54. }
  55. if (is_null($mode))
  56. {
  57. $mode = self::PARAMETER_NONE;
  58. }
  59. else if (is_string($mode) || $mode > 15)
  60. {
  61. throw new sfCommandException(sprintf('Option mode "%s" is not valid.', $mode));
  62. }
  63. $this->name = $name;
  64. $this->shortcut = $shortcut;
  65. $this->mode = $mode;
  66. $this->help = $help;
  67. $this->setDefault($default);
  68. }
  69. /**
  70. * Returns the shortcut.
  71. *
  72. * @return string The shortcut
  73. */
  74. public function getShortcut()
  75. {
  76. return $this->shortcut;
  77. }
  78. /**
  79. * Returns the 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 accept a parameter.
  89. *
  90. * @return Boolean true if parameter mode is not self::PARAMETER_NONE, false otherwise
  91. */
  92. public function acceptParameter()
  93. {
  94. return $this->isParameterRequired() || $this->isParameterOptional();
  95. }
  96. /**
  97. * Returns true if the option requires a parameter.
  98. *
  99. * @return Boolean true if parameter mode is self::PARAMETER_REQUIRED, false otherwise
  100. */
  101. public function isParameterRequired()
  102. {
  103. return self::PARAMETER_REQUIRED === (self::PARAMETER_REQUIRED & $this->mode);
  104. }
  105. /**
  106. * Returns true if the option takes an optional parameter.
  107. *
  108. * @return Boolean true if parameter mode is self::PARAMETER_OPTIONAL, false otherwise
  109. */
  110. public function isParameterOptional()
  111. {
  112. return self::PARAMETER_OPTIONAL === (self::PARAMETER_OPTIONAL & $this->mode);
  113. }
  114. /**
  115. * Returns true if the option can take multiple values.
  116. *
  117. * @return Boolean true if mode is self::IS_ARRAY, false otherwise
  118. */
  119. public function isArray()
  120. {
  121. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  122. }
  123. /**
  124. * Sets the default value.
  125. *
  126. * @param mixed $default The default value
  127. */
  128. public function setDefault($default = null)
  129. {
  130. if (self::PARAMETER_NONE === (self::PARAMETER_NONE & $this->mode) && !is_null($default))
  131. {
  132. throw new sfCommandException('Cannot set a default value when using sfCommandOption::PARAMETER_NONE mode.');
  133. }
  134. if ($this->isArray())
  135. {
  136. if (is_null($default))
  137. {
  138. $default = array();
  139. }
  140. else if (!is_array($default))
  141. {
  142. throw new sfCommandException('A default value for an array option must be an array.');
  143. }
  144. }
  145. $this->default = $this->acceptParameter() ? $default : false;
  146. }
  147. /**
  148. * Returns the default value.
  149. *
  150. * @return mixed The default value
  151. */
  152. public function getDefault()
  153. {
  154. return $this->default;
  155. }
  156. /**
  157. * Returns the help text.
  158. *
  159. * @return string The help text
  160. */
  161. public function getHelp()
  162. {
  163. return $this->help;
  164. }
  165. }