PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/ConsoleInputOption.php

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