ConsoleInputOption.php 5.4 KB

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