ConsoleInputOption.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * ConsoleInputOption file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @since CakePHP(tm) v 2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  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
  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 string|array $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. * @throws ConsoleException
  73. */
  74. public function __construct($name, $short = null, $help = '', $boolean = false, $default = '', $choices = array()) {
  75. if (is_array($name) && isset($name['name'])) {
  76. foreach ($name as $key => $value) {
  77. $this->{'_' . $key} = $value;
  78. }
  79. } else {
  80. $this->_name = $name;
  81. $this->_short = $short;
  82. $this->_help = $help;
  83. $this->_boolean = $boolean;
  84. $this->_default = $default;
  85. $this->_choices = $choices;
  86. }
  87. if (strlen($this->_short) > 1) {
  88. throw new ConsoleException(
  89. __d('cake_console', 'Short option "%s" is invalid, short options must be one letter.', $this->_short)
  90. );
  91. }
  92. }
  93. /**
  94. * Get the value of the name attribute.
  95. *
  96. * @return string Value of this->_name.
  97. */
  98. public function name() {
  99. return $this->_name;
  100. }
  101. /**
  102. * Get the value of the short attribute.
  103. *
  104. * @return string Value of this->_short.
  105. */
  106. public function short() {
  107. return $this->_short;
  108. }
  109. /**
  110. * Generate the help for this this option.
  111. *
  112. * @param integer $width The width to make the name of the option.
  113. * @return string
  114. */
  115. public function help($width = 0) {
  116. $default = $short = '';
  117. if (!empty($this->_default) && $this->_default !== true) {
  118. $default = __d('cake_console', ' <comment>(default: %s)</comment>', $this->_default);
  119. }
  120. if (!empty($this->_choices)) {
  121. $default .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
  122. }
  123. if (!empty($this->_short)) {
  124. $short = ', -' . $this->_short;
  125. }
  126. $name = sprintf('--%s%s', $this->_name, $short);
  127. if (strlen($name) < $width) {
  128. $name = str_pad($name, $width, ' ');
  129. }
  130. return sprintf('%s%s%s', $name, $this->_help, $default);
  131. }
  132. /**
  133. * Get the usage value for this option
  134. *
  135. * @return string
  136. */
  137. public function usage() {
  138. $name = empty($this->_short) ? '--' . $this->_name : '-' . $this->_short;
  139. $default = '';
  140. if (!empty($this->_default) && $this->_default !== true) {
  141. $default = ' ' . $this->_default;
  142. }
  143. if (!empty($this->_choices)) {
  144. $default = ' ' . implode('|', $this->_choices);
  145. }
  146. return sprintf('[%s%s]', $name, $default);
  147. }
  148. /**
  149. * Get the default value for this option
  150. *
  151. * @return mixed
  152. */
  153. public function defaultValue() {
  154. return $this->_default;
  155. }
  156. /**
  157. * Check if this option is a boolean option
  158. *
  159. * @return boolean
  160. */
  161. public function isBoolean() {
  162. return (bool)$this->_boolean;
  163. }
  164. /**
  165. * Check that a value is a valid choice for this option.
  166. *
  167. * @param string $value
  168. * @return boolean
  169. * @throws ConsoleException
  170. */
  171. public function validChoice($value) {
  172. if (empty($this->_choices)) {
  173. return true;
  174. }
  175. if (!in_array($value, $this->_choices)) {
  176. throw new ConsoleException(
  177. __d('cake_console', '"%s" is not a valid value for --%s. Please use one of "%s"',
  178. $value, $this->_name, implode(', ', $this->_choices)
  179. ));
  180. }
  181. return true;
  182. }
  183. /**
  184. * Append the option's xml into the parent.
  185. *
  186. * @param SimpleXmlElement $parent The parent element.
  187. * @return SimpleXmlElement The parent with this option appended.
  188. */
  189. public function xml(SimpleXmlElement $parent) {
  190. $option = $parent->addChild('option');
  191. $option->addAttribute('name', '--' . $this->_name);
  192. $short = '';
  193. if (strlen($this->_short)) {
  194. $short = $this->_short;
  195. }
  196. $option->addAttribute('short', '-' . $short);
  197. $option->addAttribute('boolean', $this->_boolean);
  198. $option->addChild('default', $this->_default);
  199. $choices = $option->addChild('choices');
  200. foreach ($this->_choices as $valid) {
  201. $choices->addChild('choice', $valid);
  202. }
  203. return $parent;
  204. }
  205. }