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.0 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 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
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • ConsoleErrorHandler
  • ConsoleInput
  • ConsoleInputArgument
  • ConsoleInputOption
  • ConsoleInputSubcommand
  • ConsoleOptionParser
  • ConsoleOutput
  • HelpFormatter
  • Shell
  • ShellDispatcher
  • TaskCollection
  1: <?php
  2: /**
  3:  * ConsoleArgumentOption 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 argument used in the command line.
 20:  * ConsoleOptionParser creates these when you use addArgument()
 21:  *
 22:  * @see ConsoleOptionParser::addArgument()
 23:  * @package       Cake.Console
 24:  */
 25: class ConsoleInputArgument {
 26: /**
 27:  * Name of the argument.
 28:  *
 29:  * @var string
 30:  */
 31:     protected $_name;
 32: 
 33: /**
 34:  * Help string
 35:  *
 36:  * @var string
 37:  */
 38:     protected $_help;
 39: 
 40: /**
 41:  * Is this option required?
 42:  *
 43:  * @var boolean
 44:  */
 45:     protected $_required;
 46: 
 47: /**
 48:  * An array of valid choices for this argument.
 49:  *
 50:  * @var array
 51:  */
 52:     protected $_choices;
 53: 
 54: /**
 55:  * Make a new Input Argument
 56:  *
 57:  * @param mixed $name The long name of the option, or an array with all the properties.
 58:  * @param string $help The help text for this option
 59:  * @param boolean $required Whether this argument is required. Missing required args will trigger exceptions
 60:  * @param array $choices Valid choices for this option.
 61:  */
 62:     public function __construct($name, $help = '', $required = false, $choices = array()) {
 63:         if (is_array($name) && isset($name['name'])) {
 64:             foreach ($name as $key => $value) {
 65:                 $this->{'_' . $key} = $value;
 66:             }
 67:         } else {
 68:             $this->_name = $name;
 69:             $this->_help = $help;
 70:             $this->_required = $required;
 71:             $this->_choices = $choices;
 72:         }
 73:     }
 74: 
 75: /**
 76:  * Get the value of the name attribute.
 77:  *
 78:  * @return string Value of this->_name.
 79:  */
 80:     public function name() {
 81:         return $this->_name;
 82:     }
 83: 
 84: /**
 85:  * Generate the help for this argument.
 86:  *
 87:  * @param integer $width The width to make the name of the option.
 88:  * @return string
 89:  */
 90:     public function help($width = 0) {
 91:         $name = $this->_name;
 92:         if (strlen($name) < $width) {
 93:             $name = str_pad($name, $width, ' ');
 94:         }
 95:         $optional = '';
 96:         if (!$this->isRequired()) {
 97:             $optional = __d('cake_console', ' <comment>(optional)</comment>');
 98:         }
 99:         if (!empty($this->_choices)) {
100:             $optional .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
101:         }
102:         return sprintf('%s%s%s', $name, $this->_help, $optional);
103:     }
104: 
105: /**
106:  * Get the usage value for this argument
107:  *
108:  * @return string
109:  */
110:     public function usage() {
111:         $name = $this->_name;
112:         if (!empty($this->_choices)) {
113:             $name = implode('|', $this->_choices);
114:         }
115:         $name = '<' . $name . '>';
116:         if (!$this->isRequired()) {
117:             $name = '[' . $name . ']';
118:         }
119:         return $name;
120:     }
121: 
122: /**
123:  * Check if this argument is a required argument
124:  *
125:  * @return boolean
126:  */
127:     public function isRequired() {
128:         return (bool) $this->_required;
129:     }
130: 
131: /**
132:  * Check that $value is a valid choice for this argument.
133:  *
134:  * @param string $value
135:  * @return boolean
136:  * @throws ConsoleException
137:  */
138:     public function validChoice($value) {
139:         if (empty($this->_choices)) {
140:             return true;
141:         }
142:         if (!in_array($value, $this->_choices)) {
143:             throw new ConsoleException(
144:                 __d('cake_console', '"%s" is not a valid value for %s. Please use one of "%s"',
145:                 $value, $this->_name, implode(', ', $this->_choices)
146:             ));
147:         }
148:         return true;
149:     }
150: 
151: /**
152:  * Append this arguments XML representation to the passed in SimpleXml object.
153:  *
154:  * @param SimpleXmlElement $parent The parent element.
155:  * @return SimpleXmlElement The parent with this argument appended.
156:  */
157:     public function xml(SimpleXmlElement $parent) {
158:         $option = $parent->addChild('argument');
159:         $option->addAttribute('name', $this->_name);
160:         $option->addAttribute('help', $this->_help);
161:         $option->addAttribute('required', $this->isRequired());
162:         $choices = $option->addChild('choices');
163:         foreach ($this->_choices as $valid) {
164:             $choices->addChild('choice', $valid);
165:         }
166:         return $parent;
167:     }
168: }
169: 
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