Skip to content

Commit

Permalink
Starting to implement options and option parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 6945379 commit d5d9adb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
26 changes: 23 additions & 3 deletions cake/console/console_option_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,34 @@ public function epilog($text = null) {
* when this option is not present.
* - `description` - Description for this option.
* - `type` - Require a certain type. Available types are `int` and `string`. If the options
* value is the wrong type an exception will be raised.
* value is the wrong type an exception will be raised. Leave undefined to accept anything.
* - `default` - The default value for this option. If not defined the default will be null.
*
* @param string $name The name you want to the value to be parsed out as when options are parsed.
* @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
* @return ConsoleOptionParser returns $this.
* @return returns $this.
*/
public function addOption($name, $params = array()) {
$defaults = array(
'shortcut' => null,
'required' => false,
'description' => '',
'type' => null,
'default' => null
);
$this->_options[$name] = array_merge($defaults, $params);
return $this;
}

/**
* Parse the argv array into a set of params and args.
*
* @param array $argv Array of args (argv) to parse
* @return Array array($params, $args)
*/
public function parse($argv) {
$params = $args = array();

return array($params, $args);
}
}
19 changes: 17 additions & 2 deletions cake/tests/cases/console/console_option_parser.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,27 @@ function testEpilog() {
}

/**
* test adding an option.
* test adding an option returns self.
*
* @return void
*/
function testAddOption() {
function testAddOptionReturnSelf() {
$parser = new ConsoleOptionParser();
$result = $parser->addOption('test');
$this->assertEquals($parser, $result, 'Did not return $this from addOption');
}

/**
* test adding an option and using the shortcut value for parsing.
*
* @return void
*/
function testAddOptionShortcut() {
$parser = new ConsoleOptionParser();
$parser->addOption('test', array(
'shortcut' => 't'
));
$result = $parser->parse(array('-t', 'value'));
$this->assertEqual(array('test' => 'value'), $result[0], 'Short parameter did not parse out');
}
}

0 comments on commit d5d9adb

Please sign in to comment.