Skip to content

Commit

Permalink
Merge pull request #104 from Luxian/master
Browse files Browse the repository at this point in the history
Bring back default values and fix tests
  • Loading branch information
danielbachhuber authored Feb 15, 2017
2 parents 28880df + f5080ac commit 6d231e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
23 changes: 21 additions & 2 deletions lib/cli/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ public function parse() {
$this->_parsed = array();
$this->_lexer = new Lexer($this->_input);

$this->_applyDefaults();

foreach ($this->_lexer as $argument) {
if ($this->_parseFlag($argument)) {
continue;
Expand All @@ -408,6 +410,24 @@ public function parse() {
}
}

/**
* This applies the default values, if any, of all of the
* flags and options, so that if there is a default value
* it will be available.
*/
private function _applyDefaults() {
foreach($this->_flags as $flag => $settings) {
$this[$flag] = $settings['default'];
}

foreach($this->_options as $option => $settings) {
// If the default is 0 we should still let it be set.
if (!empty($settings['default']) || $settings['default'] === 0) {
$this[$option] = $settings['default'];
}
}
}

private function _warn($message) {
trigger_error('[' . __CLASS__ .'] ' . $message, E_USER_WARNING);
}
Expand Down Expand Up @@ -439,7 +459,7 @@ private function _parseOption($option) {
if ($this->_lexer->end() || !$this->_lexer->peek->isValue) {
$optionSettings = $this->getOption($option->key);

if (empty($optionSettings['default'])) {
if (empty($optionSettings['default']) && $optionSettings !== 0) {
// Oops! Got no value and no default , throw a warning and continue.
$this->_warn('no value given for ' . $option->raw);
$this[$option->key] = null;
Expand All @@ -466,4 +486,3 @@ private function _parseOption($option) {
return true;
}
}

23 changes: 21 additions & 2 deletions tests/test-arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testAddOptions()
}

/**
* Data provider with valid fags and options
* Data provider with valid args and options
*
* @return array set of args and expected parsed values
*/
Expand Down Expand Up @@ -206,6 +206,16 @@ public function settingsWithMissingOptionsWithDefault()
);
}

public function settingsWithNoOptionsWithDefault()
{
return array(
array(
array(),
array('flag1' => false, 'flag2' => false, 'option2' => 'some default value')
)
);
}

/**
* Generic private testParse method.
*
Expand All @@ -221,7 +231,7 @@ private function _testParse($cliParams, $expectedValues)

foreach ($expectedValues as $name => $value) {
if ($args->isFlag($name)) {
$this->assertTrue($args[$name]);
$this->assertEquals($value, $args[$name]);
}

if ($args->isOption($name)) {
Expand Down Expand Up @@ -262,4 +272,13 @@ public function testParseWithMissingOptionsWithDefault($cliParams, $expectedValu
{
$this->_testParse($cliParams, $expectedValues);
}

/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @dataProvider settingsWithNoOptionsWithDefault
*/
public function testParseWithNoOptionsWithDefault($cliParams, $expectedValues) {
$this->_testParse($cliParams, $expectedValues);
}
}

0 comments on commit 6d231e5

Please sign in to comment.