ConsoleInputOption.php 5.3 KB

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