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: class ConsoleInputOption {
27:
28: 29: 30: 31: 32:
33: protected $_name;
34:
35: 36: 37: 38: 39:
40: protected $_short;
41:
42: 43: 44: 45: 46:
47: protected $_help;
48:
49: 50: 51: 52: 53:
54: protected $_boolean;
55:
56: 57: 58: 59: 60:
61: protected $_default;
62:
63: 64: 65: 66: 67:
68: protected $_choices;
69:
70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 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: 103: 104: 105:
106: public function name() {
107: return $this->_name;
108: }
109:
110: 111: 112: 113: 114:
115: public function short() {
116: return $this->_short;
117: }
118:
119: 120: 121: 122: 123: 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: 145: 146: 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: 162: 163: 164:
165: public function defaultValue() {
166: return $this->_default;
167: }
168:
169: 170: 171: 172: 173:
174: public function isBoolean() {
175: return (bool)$this->_boolean;
176: }
177:
178: 179: 180: 181: 182: 183: 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: 200: 201: 202: 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: