CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs