ConsoleInputOption.php 6.9 KB

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