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