1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: 21: 22: 23: 24: 25: 26:
27: class ConsoleInputOption {
28:
29: 30: 31: 32: 33:
34: protected $_name;
35:
36: 37: 38: 39: 40:
41: protected $_short;
42:
43: 44: 45: 46: 47:
48: protected $_help;
49:
50: 51: 52: 53: 54:
55: protected $_boolean;
56:
57: 58: 59: 60: 61:
62: protected $_default;
63:
64: 65: 66: 67: 68:
69: protected $_choices;
70:
71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 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: 104: 105: 106:
107: public function name() {
108: return $this->_name;
109: }
110:
111: 112: 113: 114: 115:
116: public function short() {
117: return $this->_short;
118: }
119:
120: 121: 122: 123: 124: 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: 146: 147: 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: 163: 164: 165:
166: public function defaultValue() {
167: return $this->_default;
168: }
169:
170: 171: 172: 173: 174:
175: public function isBoolean() {
176: return (bool)$this->_boolean;
177: }
178:
179: 180: 181: 182: 183: 184: 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: 201: 202: 203: 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: