Skip to content

Commit

Permalink
Merge pull request #98 from wp-cli/revert-97-defaults-fix
Browse files Browse the repository at this point in the history
Revert "always populate parsed arguments with default values"
  • Loading branch information
danielbachhuber authored Jul 11, 2016
2 parents 6f8e13b + 4c140c2 commit 28880df
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 41 deletions.
23 changes: 2 additions & 21 deletions lib/cli/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,6 @@ 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 @@ -410,24 +408,6 @@ 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 @@ -459,7 +439,7 @@ private function _parseOption($option) {
if ($this->_lexer->end() || !$this->_lexer->peek->isValue) {
$optionSettings = $this->getOption($option->key);

if (empty($optionSettings['default']) && $optionSettings !== 0) {
if (empty($optionSettings['default'])) {
// 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 @@ -486,3 +466,4 @@ private function _parseOption($option) {
return true;
}
}

21 changes: 1 addition & 20 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 args and options
* Data provider with valid fags and options
*
* @return array set of args and expected parsed values
*/
Expand Down Expand Up @@ -206,16 +206,6 @@ public function settingsWithMissingOptionsWithDefault()
);
}

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

/**
* Generic private testParse method.
*
Expand Down Expand Up @@ -272,13 +262,4 @@ 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 28880df

Please sign in to comment.