Skip to content

Commit 7b1b835

Browse files
committed
Adding very basic implementation for parsing long and short options.
1 parent d5d9adb commit 7b1b835

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

cake/console/console_option_parser.php

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,27 @@ public function epilog($text = null) {
9898
* when this option is not present.
9999
* - `description` - Description for this option.
100100
* - `type` - Require a certain type. Available types are `int` and `string`. If the options
101-
* value is the wrong type an exception will be raised. Leave undefined to accept anything.
102-
* - `default` - The default value for this option. If not defined the default will be null.
101+
* value is the wrong type an exception will be raised. Leave undefined to accept anything.
102+
* - `default` - The default value for this option. If not defined the default will be true.
103103
*
104104
* @param string $name The long name you want to the value to be parsed out as when options are parsed.
105105
* @param array $params An array of parameters that define the behavior of the option
106106
* @return returns $this.
107107
*/
108108
public function addOption($name, $params = array()) {
109109
$defaults = array(
110+
'name' => $name,
110111
'shortcut' => null,
111112
'required' => false,
112113
'description' => '',
113114
'type' => null,
114-
'default' => null
115+
'default' => true
115116
);
116-
$this->_options[$name] = array_merge($defaults, $params);
117+
$options = array_merge($defaults, $params);
118+
$this->_options[$name] = $options;
119+
if (!empty($options['shortcut'])) {
120+
$this->_options[$options['shortcut']] = $options;
121+
}
117122
return $this;
118123
}
119124

@@ -125,7 +130,67 @@ public function addOption($name, $params = array()) {
125130
*/
126131
public function parse($argv) {
127132
$params = $args = array();
128-
133+
$this->_tokens = $argv;
134+
while ($token = array_shift($this->_tokens)) {
135+
if (substr($token, 0, 2) == '--') {
136+
$params = $this->_parseLongOption($token, $params);
137+
} elseif (substr($token, 0, 1) == '-') {
138+
$params = $this->_parseShortOption($token, $params);
139+
}
140+
}
129141
return array($params, $args);
130142
}
143+
144+
/**
145+
* Parse the value for a long option out of $this->_tokens
146+
*
147+
* @param string $option The option to parse.
148+
* @param array $params The params to append the parsed value into
149+
* @return array Params with $option added in.
150+
*/
151+
protected function _parseLongOption($option, $params) {
152+
$name = substr($option, 2);
153+
return $this->_parseOptionName($name, $params);
154+
}
155+
156+
/**
157+
* Parse the value for a short option out of $this->_tokens
158+
*
159+
* @param string $option The option to parse.
160+
* @param array $params The params to append the parsed value into
161+
* @return array Params with $option added in.
162+
*/
163+
protected function _parseShortOption($option, $params) {
164+
$key = substr($option, 1);
165+
$name = $this->_options[$key]['name'];
166+
return $this->_parseOptionName($name, $params);
167+
}
168+
169+
/**
170+
* Parse an option by its name index.
171+
*
172+
* @param string $option The option to parse.
173+
* @param array $params The params to append the parsed value into
174+
* @return array Params with $option added in.
175+
*/
176+
protected function _parseOptionName($name, $params) {
177+
$definition = $this->_options[$name];
178+
$nextValue = $this->_nextToken();
179+
if (empty($nextValue)) {
180+
$value = $definition['default'];
181+
} else if ($nextValue{0} != '-') {
182+
$value = $nextValue;
183+
}
184+
$params[$name] = $value;
185+
return $params;
186+
}
187+
188+
/**
189+
* Find the next token in the argv set.
190+
*
191+
* @return next token or ''
192+
*/
193+
protected function _nextToken() {
194+
return isset($this->_tokens[0]) ? $this->_tokens[0] : '';
195+
}
131196
}

cake/tests/cases/console/console_option_parser.test.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@ function testAddOptionReturnSelf() {
5959
$this->assertEquals($parser, $result, 'Did not return $this from addOption');
6060
}
6161

62+
/**
63+
* test adding an option and using the long value for parsing.
64+
*
65+
* @return void
66+
*/
67+
function testAddOptionLong() {
68+
$parser = new ConsoleOptionParser();
69+
$parser->addOption('test', array(
70+
'shortcut' => 't'
71+
));
72+
$result = $parser->parse(array('--test', 'value'));
73+
$this->assertEqual(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
74+
}
75+
76+
/**
77+
* test adding an option and using the default.
78+
*
79+
* @return void
80+
*/
81+
function testAddOptionDefault() {
82+
$parser = new ConsoleOptionParser();
83+
$parser->addOption('test', array(
84+
'default' => 'default value',
85+
));
86+
$result = $parser->parse(array('--test'));
87+
$this->assertEqual(array('test' => 'default value'), $result[0], 'Default value did not parse out');
88+
}
89+
6290
/**
6391
* test adding an option and using the shortcut value for parsing.
6492
*

0 commit comments

Comments
 (0)