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