ConsoleInputOption.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * ConsoleInputOption file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.console.libs
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * An object to represent a single option used in the command line.
  21. * ConsoleOptionParser creates these when you use addOption()
  22. *
  23. * @see ConsoleOptionParser::addOption()
  24. * @package cake.console.libs
  25. */
  26. class ConsoleInputOption {
  27. /**
  28. * Name of the option
  29. *
  30. * @var string
  31. */
  32. protected $_name;
  33. /**
  34. * Short (1 character) alias for the option.
  35. *
  36. * @var string
  37. */
  38. protected $_short;
  39. /**
  40. * Help text for the option.
  41. *
  42. * @var string
  43. */
  44. protected $_help;
  45. /**
  46. * Is the option a boolean option. Boolean options do not consume a parameter.
  47. *
  48. * @var boolean
  49. */
  50. protected $_boolean;
  51. /**
  52. * Default value for the option
  53. *
  54. * @var mixed
  55. */
  56. protected $_default;
  57. /**
  58. * An array of choices for the option.
  59. *
  60. * @var array
  61. */
  62. protected $_choices;
  63. /**
  64. * Make a new Input Option
  65. *
  66. * @param mixed $name The long name of the option, or an array with all the properties.
  67. * @param string $short The short alias for this option
  68. * @param string $help The help text for this option
  69. * @param boolean $boolean Whether this option is a boolean option. Boolean options don't consume extra tokens
  70. * @param string $default The default value for this option.
  71. * @param array $choices Valid choices for this option.
  72. */
  73. public function __construct($name, $short = null, $help = '', $boolean = false, $default = '', $choices = array()) {
  74. if (is_array($name) && isset($name['name'])) {
  75. foreach ($name as $key => $value) {
  76. $this->{'_' . $key} = $value;
  77. }
  78. } else {
  79. $this->_name = $name;
  80. $this->_short = $short;
  81. $this->_help = $help;
  82. $this->_boolean = $boolean;
  83. $this->_default = $default;
  84. $this->_choices = $choices;
  85. }
  86. }
  87. /**
  88. * Get the value of the name attribute.
  89. *
  90. * @return string Value of this->_name.
  91. */
  92. public function name() {
  93. return $this->_name;
  94. }
  95. /**
  96. * Get the value of the short attribute.
  97. *
  98. * @return string Value of this->_short.
  99. */
  100. public function short() {
  101. return $this->_short;
  102. }
  103. /**
  104. * Generate the help for this this option.
  105. *
  106. * @param int $width The width to make the name of the option.
  107. * @return string
  108. */
  109. public function help($width = 0) {
  110. $default = $short = '';
  111. if (!empty($this->_default) && $this->_default !== true) {
  112. $default = __d('cake_console', ' <comment>(default: %s)</comment>', $this->_default);
  113. }
  114. if (!empty($this->_choices)) {
  115. $default .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
  116. }
  117. if (!empty($this->_short)) {
  118. $short = ', -' . $this->_short;
  119. }
  120. $name = sprintf('--%s%s', $this->_name, $short);
  121. if (strlen($name) < $width) {
  122. $name = str_pad($name, $width, ' ');
  123. }
  124. return sprintf('%s%s%s', $name, $this->_help, $default);
  125. }
  126. /**
  127. * Get the usage value for this option
  128. *
  129. * @return string
  130. */
  131. public function usage() {
  132. $name = empty($this->_short) ? '--' . $this->_name : '-' . $this->_short;
  133. $default = '';
  134. if (!empty($this->_default) && $this->_default !== true) {
  135. $default = ' ' . $this->_default;
  136. }
  137. if (!empty($this->_choices)) {
  138. $default = ' ' . implode('|', $this->_choices);
  139. }
  140. return sprintf('[%s%s]', $name, $default);
  141. }
  142. /**
  143. * Get the default value for this option
  144. *
  145. * @return mixed
  146. */
  147. public function defaultValue() {
  148. return $this->_default;
  149. }
  150. /**
  151. * Check if this option is a boolean option
  152. *
  153. * @return boolean
  154. */
  155. public function isBoolean() {
  156. return (bool) $this->_boolean;
  157. }
  158. /**
  159. * Check that a value is a valid choice for this option.
  160. *
  161. * @return boolean
  162. */
  163. public function validChoice($value) {
  164. if (empty($this->_choices)) {
  165. return true;
  166. }
  167. if (!in_array($value, $this->_choices)) {
  168. throw new ConsoleException(
  169. __d('cake_console', '"%s" is not a valid value for --%s. Please use one of "%s"',
  170. $value, $this->_name, implode(', ', $this->_choices)
  171. ));
  172. }
  173. return true;
  174. }
  175. /**
  176. * Append the option's xml into the parent.
  177. *
  178. * @param SimpleXmlElement The parent element.
  179. * @return SimpleXmlElement The parent with this option appended.
  180. */
  181. public function xml(SimpleXmlElement $parent) {
  182. $option = $parent->addChild('option');
  183. $option->addAttribute('name', '--' . $this->_name);
  184. $short = '';
  185. if (strlen($this->_short)) {
  186. $short = $this->_short;
  187. }
  188. $option->addAttribute('short', '-' . $short);
  189. $option->addAttribute('boolean', $this->_boolean);
  190. $option->addChild('default', $this->_default);
  191. $choices = $option->addChild('choices');
  192. foreach ($this->_choices as $valid) {
  193. $choices->addChild('choice', $valid);
  194. }
  195. return $parent;
  196. }
  197. }