Skip to content

Commit

Permalink
Adding default values into the parsed output.
Browse files Browse the repository at this point in the history
This makes it so options with default values are always part of the parsed options regardless of whether or not they were included.  This will allow shells to include less logic checking for existence of parameters.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 3585620 commit b328276
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cake/console/console_option_parser.php
Expand Up @@ -130,10 +130,12 @@ public function __construct($command = null, $defaultOptions = true) {
if ($defaultOptions) {
$this->addOption('verbose', array(
'short' => 'v',
'help' => __('Enable verbose output.')
'help' => __('Enable verbose output.'),
'boolean' => true
))->addOption('quiet', array(
'short' => 'q',
'help' => __('Enable quiet output.')
'help' => __('Enable quiet output.'),
'boolean' => true
));
}
}
Expand Down Expand Up @@ -249,7 +251,7 @@ public function epilog($text = null) {
* - `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. Defaults are added into the parsed params when the
* attached option is not provided. Using default and boolean together will not work.
* attached option is not provided or has no value. Using default and boolean together will not work.
* are added into the parsed parameters when the option is undefined.
* - `boolean` - The option uses no value, its just a boolean switch. Defaults to false.
* If an option is defined as boolean, it will always be added to the parsed params. If no present
Expand Down Expand Up @@ -442,6 +444,11 @@ 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();
}
}
return array($params, $args);
}

Expand Down
7 changes: 7 additions & 0 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -105,6 +105,13 @@ function testAddOptionDefault() {
));
$result = $parser->parse(array('--test'));
$this->assertEquals(array('test' => 'default value'), $result[0], 'Default value did not parse out');

$parser = new ConsoleOptionParser();
$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');
}

/**
Expand Down

0 comments on commit b328276

Please sign in to comment.