Base for a static organization website

ConsoleInputOption.php 5.3KB

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