mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Adding support for boolean options.
This commit is contained in:
parent
3e402e2dfb
commit
505e59ac66
2 changed files with 22 additions and 2 deletions
|
@ -117,6 +117,7 @@ class ConsoleOptionParser {
|
|||
* - `short` - The single letter variant for this option, leave undefined for none.
|
||||
* - `help` - Help text for this option. Used when generating help for the option.
|
||||
* - `default` - The default value for this option. If not defined the default will be true.
|
||||
* - `boolean` - The option uses no value, its just a boolean switch. Defaults to false.
|
||||
*
|
||||
* @param string $name The long name you want to the value to be parsed out as when options are parsed.
|
||||
* @param array $params An array of parameters that define the behavior of the option
|
||||
|
@ -127,7 +128,8 @@ class ConsoleOptionParser {
|
|||
'name' => $name,
|
||||
'short' => null,
|
||||
'help' => '',
|
||||
'default' => true
|
||||
'default' => true,
|
||||
'boolean' => false
|
||||
);
|
||||
$options = array_merge($defaults, $params);
|
||||
$this->_options[$name] = $options;
|
||||
|
@ -245,7 +247,8 @@ class ConsoleOptionParser {
|
|||
protected function _parseOptionName($name, $params) {
|
||||
$definition = $this->_options[$name];
|
||||
$nextValue = $this->_nextToken();
|
||||
if (!empty($nextValue) && $nextValue{0} != '-') {
|
||||
if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') {
|
||||
array_shift($this->_tokens);
|
||||
$value = $nextValue;
|
||||
} else {
|
||||
$value = $definition['default'];
|
||||
|
|
|
@ -150,6 +150,22 @@ class ConsoleOptionParserTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
|
||||
}
|
||||
|
||||
/**
|
||||
* test that boolean options work
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testOptionWithBooleanParam() {
|
||||
$parser = new ConsoleOptionParser();
|
||||
$parser->addOption('no-commit', array('boolean' => true))
|
||||
->addOption('table', array('short' => 't'));
|
||||
|
||||
$result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
|
||||
$expected = array(array('table' => 'posts', 'no-commit' => true), array('arg1', 'arg2'));
|
||||
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* test positional argument parsing.
|
||||
*
|
||||
|
@ -189,6 +205,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
|
|||
$expected = array('one', 'two');
|
||||
$result = $parser->parse($expected);
|
||||
$this->assertEquals($expected, $result[1], 'Arguments are not as expected');
|
||||
|
||||
$result = $parser->parse(array('one', 'two', 'three'));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue