1. sfCommandOption.class.php
  2. /** * Represents a command line option. * * @package symfony * @subpackage command * @author Fabien Potencier * @version SVN: $Id: sfCommandOption.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfCommandOption
  4. {
  5. const PARAMETER_NONE = 1;
  6. const PARAMETER_REQUIRED = 2;
  7. const PARAMETER_OPTIONAL = 4;
  8. const IS_ARRAY = 8;
  9. protected
  10. $name = null,
  11. $shortcut = null,
  12. $mode = null,
  13. $default = null,
  14. $help = '';
  15. /**
  16. * Constructor.
  17. *
  18. * @param string $name The option name
  19. * @param string $shortcut The shortcut (can be null)
  20. * @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL
  21. * @param string $help A help text
  22. * @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE)
  23. */
  24. public function __construct($name, $shortcut = null, $mode = null, $help = '', $default = null)
  25. {
  26. if ('--' == substr($name, 0, 2))
  27. {
  28. $name = substr($name, 2);
  29. }
  30. if (empty($shortcut))
  31. {
  32. $shortcut = null;
  33. }
  34. if (null !== $shortcut)
  35. {
  36. if ('-' == $shortcut[0])
  37. {
  38. $shortcut = substr($shortcut, 1);
  39. }
  40. }
  41. if (null === $mode)
  42. {
  43. $mode = self::PARAMETER_NONE;
  44. }
  45. else if (is_string($mode) || $mode > 15)
  46. {
  47. throw new sfCommandException(sprintf('Option mode "%s" is not valid.', $mode));
  48. }
  49. $this->name = $name;
  50. $this->shortcut = $shortcut;
  51. $this->mode = $mode;
  52. $this->help = $help;
  53. $this->setDefault($default);
  54. }
  55. /**
  56. * Returns the shortcut.
  57. *
  58. * @return string The shortcut
  59. */
  60. public function getShortcut()
  61. {
  62. return $this->shortcut;
  63. }
  64. /**
  65. * Returns the name.
  66. *
  67. * @return string The name
  68. */
  69. public function getName()
  70. {
  71. return $this->name;
  72. }
  73. /**
  74. * Returns true if the option accept a parameter.
  75. *
  76. * @return Boolean true if parameter mode is not self::PARAMETER_NONE, false otherwise
  77. */
  78. public function acceptParameter()
  79. {
  80. return $this->isParameterRequired() || $this->isParameterOptional();
  81. }
  82. /**
  83. * Returns true if the option requires a parameter.
  84. *
  85. * @return Boolean true if parameter mode is self::PARAMETER_REQUIRED, false otherwise
  86. */
  87. public function isParameterRequired()
  88. {
  89. return self::PARAMETER_REQUIRED === (self::PARAMETER_REQUIRED & $this->mode);
  90. }
  91. /**
  92. * Returns true if the option takes an optional parameter.
  93. *
  94. * @return Boolean true if parameter mode is self::PARAMETER_OPTIONAL, false otherwise
  95. */
  96. public function isParameterOptional()
  97. {
  98. return self::PARAMETER_OPTIONAL === (self::PARAMETER_OPTIONAL & $this->mode);
  99. }
  100. /**
  101. * Returns true if the option can take multiple values.
  102. *
  103. * @return Boolean true if mode is self::IS_ARRAY, false otherwise
  104. */
  105. public function isArray()
  106. {
  107. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  108. }
  109. /**
  110. * Sets the default value.
  111. *
  112. * @param mixed $default The default value
  113. */
  114. public function setDefault($default = null)
  115. {
  116. if (self::PARAMETER_NONE === (self::PARAMETER_NONE & $this->mode) && null !== $default)
  117. {
  118. throw new sfCommandException('Cannot set a default value when using sfCommandOption::PARAMETER_NONE mode.');
  119. }
  120. if ($this->isArray())
  121. {
  122. if (null === $default)
  123. {
  124. $default = array();
  125. }
  126. else if (!is_array($default))
  127. {
  128. throw new sfCommandException('A default value for an array option must be an array.');
  129. }
  130. }
  131. $this->default = $this->acceptParameter() ? $default : false;
  132. }
  133. /**
  134. * Returns the default value.
  135. *
  136. * @return mixed The default value
  137. */
  138. public function getDefault()
  139. {
  140. return $this->default;
  141. }
  142. /**
  143. * Returns the help text.
  144. *
  145. * @return string The help text
  146. */
  147. public function getHelp()
  148. {
  149. return $this->help;
  150. }
  151. }

Debug toolbar