Skip to content

Commit

Permalink
Making boolean switches behave.
Browse files Browse the repository at this point in the history
Boolean switches always show up in the parsed options.  When left undefined they insert a false, and when included they insert a true.  This makes working with them require less checks.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent b328276 commit 1009069
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 36 deletions.
11 changes: 9 additions & 2 deletions cake/console/console_option_parser.php
Expand Up @@ -445,8 +445,15 @@ public function parse($argv, $command = null) {
}
}
foreach ($this->_options as $option) {
if ($option->defaultValue() !== null && !isset($params[$option->name()]) && !$option->isBoolean()) {
$params[$option->name()] = $option->defaultValue();
$name = $option->name();
$isBoolean = $option->isBoolean();
$default = $option->defaultValue();

if ($default !== null && !isset($params[$name]) && !$isBoolean) {
$params[$name] = $default;
}
if ($isBoolean && !isset($params[$name])) {
$params[$name] = false;
}
}
return array($params, $args);
Expand Down
2 changes: 1 addition & 1 deletion cake/console/libs/shell.php
Expand Up @@ -331,7 +331,7 @@ public function runCommand($command, $argv) {
if (($isTask || $isMethod || $isMain) && $command !== 'execute' ) {
$this->startup();
}
if (isset($this->params['help'])) {
if (!empty($this->params['help'])) {
return $this->out($this->OptionParser->help($command));
}

Expand Down
4 changes: 3 additions & 1 deletion cake/console/libs/testsuite.php
Expand Up @@ -104,7 +104,9 @@ protected function parseArgs() {
*/
protected function runnerOptions() {
$options = array();
foreach ($this->params as $param => $value) {
$params = $this->params;
unset($params['help']);
foreach ($params as $param => $value) {
$options[] = '--' . $param;
if (is_string($value)) {
$options[] = $value;
Expand Down
89 changes: 57 additions & 32 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -28,7 +28,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
* @return void
*/
function testDescription() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->description('A test');

$this->assertEquals($parser, $result, 'Setting description is not chainable');
Expand All @@ -44,7 +44,7 @@ function testDescription() {
* @return void
*/
function testEpilog() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->epilog('A test');

$this->assertEquals($parser, $result, 'Setting epilog is not chainable');
Expand All @@ -60,7 +60,7 @@ function testEpilog() {
* @return void
*/
function testAddOptionReturnSelf() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->addOption('test');
$this->assertEquals($parser, $result, 'Did not return $this from addOption');
}
Expand All @@ -71,12 +71,12 @@ function testAddOptionReturnSelf() {
* @return void
*/
function testAddOptionLong() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array(
'short' => 't'
));
$result = $parser->parse(array('--test', 'value'));
$this->assertEquals(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
}

/**
Expand All @@ -85,12 +85,12 @@ function testAddOptionLong() {
* @return void
*/
function testAddOptionLongEquals() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array(
'short' => 't'
));
$result = $parser->parse(array('--test=value'));
$this->assertEquals(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
}

/**
Expand All @@ -99,19 +99,19 @@ function testAddOptionLongEquals() {
* @return void
*/
function testAddOptionDefault() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array(
'default' => 'default value',
));
$result = $parser->parse(array('--test'));
$this->assertEquals(array('test' => 'default value'), $result[0], 'Default value did not parse out');
$this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');

$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array(
'default' => 'default value',
));
$result = $parser->parse(array());
$this->assertEquals(array('test' => 'default value'), $result[0], 'Default value did not parse out');
$this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
}

/**
Expand All @@ -120,12 +120,32 @@ function testAddOptionDefault() {
* @return void
*/
function testAddOptionShort() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array(
'short' => 't'
));
$result = $parser->parse(array('-t', 'value'));
$this->assertEquals(array('test' => 'value'), $result[0], 'Short parameter did not parse out');
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Short parameter did not parse out');
}

/**
* test adding and using boolean options.
*
* @return void
*/
function testAddOptionBoolean() {
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array(
'boolean' => true,
));

$result = $parser->parse(array('--test', 'value'));
$expected = array(array('test' => true, 'help' => false), array('value'));
$this->assertEquals($expected, $result);

$result = $parser->parse(array('value'));
$expected = array(array('test' => false, 'help' => false), array('value'));
$this->assertEquals($expected, $result);
}

/**
Expand All @@ -134,13 +154,13 @@ function testAddOptionShort() {
* @return void
*/
function testAddOptionMultipleShort() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array('short' => 't'))
->addOption('file', array('short' => 'f'))
->addOption('output', array('short' => 'o'));

$result = $parser->parse(array('-o', '-t', '-f'));
$expected = array('file' => true, 'test' => true, 'output' => true);
$expected = array('file' => true, 'test' => true, 'output' => true, 'help' => false);
$this->assertEquals($expected, $result[0], 'Short parameter did not parse out');

$result = $parser->parse(array('-otf'));
Expand All @@ -153,13 +173,13 @@ function testAddOptionMultipleShort() {
* @return void
*/
function testMultipleOptions() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test')
->addOption('connection')
->addOption('table', array('short' => 't'));

$result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
$expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres');
$expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false);
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
}

Expand All @@ -186,12 +206,12 @@ function testAddOptions() {
* @return void
*/
function testOptionWithBooleanParam() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$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'));
$expected = array(array('table' => 'posts', 'no-commit' => true, 'help' => false), array('arg1', 'arg2'));
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
}

Expand All @@ -201,7 +221,7 @@ function testOptionWithBooleanParam() {
* @expectedException InvalidArgumentException
*/
function testOptionThatDoesNotExist() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('no-commit', array('boolean' => true));

$result = $parser->parse(array('--fail', 'other'));
Expand All @@ -214,11 +234,11 @@ function testOptionThatDoesNotExist() {
* @return void
*/
function testOptionWithChoices() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('name', array('choices' => array('mark', 'jose')));

$result = $parser->parse(array('--name', 'mark'));
$expected = array('name' => 'mark');
$expected = array('name' => 'mark', 'help' => false);
$this->assertEquals($expected, $result[0], 'Got the correct value.');

$result = $parser->parse(array('--name', 'jimmy'));
Expand All @@ -230,7 +250,7 @@ function testOptionWithChoices() {
* @return void
*/
function testPositionalArgument() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->addArgument('name', array('help' => 'An argument'));
$this->assertEquals($parser, $result, 'Should returnn this');
}
Expand All @@ -241,7 +261,7 @@ function testPositionalArgument() {
* @return void
*/
function testPositionalArgOverwrite() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addArgument('name', array('help' => 'An argument'))
->addArgument('other', array('index' => 0));

Expand All @@ -256,7 +276,7 @@ function testPositionalArgOverwrite() {
* @return void
*/
function testParseArgumentTooMany() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addArgument('name', array('help' => 'An argument'))
->addArgument('other');

Expand All @@ -274,7 +294,7 @@ function testParseArgumentTooMany() {
* @return void
*/
function testPositionalArgNotEnough() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addArgument('name', array('required' => true))
->addArgument('other', array('required' => true));

Expand All @@ -288,7 +308,7 @@ function testPositionalArgNotEnough() {
* @return void
*/
function testPositionalArgWithChoices() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addArgument('name', array('choices' => array('mark', 'jose')))
->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
->addArgument('weapon', array('choices' => array('gun', 'sword')));
Expand All @@ -306,7 +326,7 @@ function testPositionalArgWithChoices() {
* @return void
*/
function testAddArguments() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->addArguments(array(
'name' => array('help' => 'The name'),
'other' => array('help' => 'The other arg')
Expand All @@ -323,7 +343,7 @@ function testAddArguments() {
* @return void
*/
function testSubcommand() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->addSubcommand('initdb', array(
'help' => 'Initialize the database'
));
Expand All @@ -336,7 +356,7 @@ function testSubcommand() {
* @return void
*/
function testAddSubcommands() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$result = $parser->addSubcommands(array(
'initdb' => array('help' => 'Initialize the database'),
'create' => array('help' => 'Create something')
Expand Down Expand Up @@ -589,7 +609,7 @@ function testBuildFromArray() {
* @return void
*/
function testParsingWithSubParser() {
$parser = new ConsoleOptionParser();
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('primary')
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
->addArgument('two', array('required' => true))
Expand All @@ -606,7 +626,12 @@ function testParsingWithSubParser() {
));

$result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
$expected = array(array('secondary' => true, 'fourth' => '4'), array('c'));
$expected = array(array(
'secondary' => true,
'fourth' => '4',
'help' => false,
'verbose' => false,
'quiet' => false), array('c'));
$this->assertEquals($expected, $result, 'Sub parser did not parse request.');
}
}

0 comments on commit 1009069

Please sign in to comment.