Skip to content

Commit 505e59a

Browse files
committed
Adding support for boolean options.
1 parent 3e402e2 commit 505e59a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cake/console/console_option_parser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function epilog($text = null) {
117117
* - `short` - The single letter variant for this option, leave undefined for none.
118118
* - `help` - Help text for this option. Used when generating help for the option.
119119
* - `default` - The default value for this option. If not defined the default will be true.
120+
* - `boolean` - The option uses no value, its just a boolean switch. Defaults to false.
120121
*
121122
* @param string $name The long name you want to the value to be parsed out as when options are parsed.
122123
* @param array $params An array of parameters that define the behavior of the option
@@ -127,7 +128,8 @@ public function addOption($name, $params = array()) {
127128
'name' => $name,
128129
'short' => null,
129130
'help' => '',
130-
'default' => true
131+
'default' => true,
132+
'boolean' => false
131133
);
132134
$options = array_merge($defaults, $params);
133135
$this->_options[$name] = $options;
@@ -245,7 +247,8 @@ protected function _parseShortOption($option, $params) {
245247
protected function _parseOptionName($name, $params) {
246248
$definition = $this->_options[$name];
247249
$nextValue = $this->_nextToken();
248-
if (!empty($nextValue) && $nextValue{0} != '-') {
250+
if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') {
251+
array_shift($this->_tokens);
249252
$value = $nextValue;
250253
} else {
251254
$value = $definition['default'];

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ function testMultipleOptions() {
150150
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
151151
}
152152

153+
/**
154+
* test that boolean options work
155+
*
156+
* @return void
157+
*/
158+
function testOptionWithBooleanParam() {
159+
$parser = new ConsoleOptionParser();
160+
$parser->addOption('no-commit', array('boolean' => true))
161+
->addOption('table', array('short' => 't'));
162+
163+
$result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
164+
$expected = array(array('table' => 'posts', 'no-commit' => true), array('arg1', 'arg2'));
165+
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
166+
167+
}
168+
153169
/**
154170
* test positional argument parsing.
155171
*
@@ -189,6 +205,7 @@ function testParseArgument() {
189205
$expected = array('one', 'two');
190206
$result = $parser->parse($expected);
191207
$this->assertEquals($expected, $result[1], 'Arguments are not as expected');
208+
192209
$result = $parser->parse(array('one', 'two', 'three'));
193210
}
194211
}

0 commit comments

Comments
 (0)