Skip to content

Commit 6d231e5

Browse files
Merge pull request #104 from Luxian/master
Bring back default values and fix tests
2 parents 28880df + f5080ac commit 6d231e5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/cli/Arguments.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ public function parse() {
392392
$this->_parsed = array();
393393
$this->_lexer = new Lexer($this->_input);
394394

395+
$this->_applyDefaults();
396+
395397
foreach ($this->_lexer as $argument) {
396398
if ($this->_parseFlag($argument)) {
397399
continue;
@@ -408,6 +410,24 @@ public function parse() {
408410
}
409411
}
410412

413+
/**
414+
* This applies the default values, if any, of all of the
415+
* flags and options, so that if there is a default value
416+
* it will be available.
417+
*/
418+
private function _applyDefaults() {
419+
foreach($this->_flags as $flag => $settings) {
420+
$this[$flag] = $settings['default'];
421+
}
422+
423+
foreach($this->_options as $option => $settings) {
424+
// If the default is 0 we should still let it be set.
425+
if (!empty($settings['default']) || $settings['default'] === 0) {
426+
$this[$option] = $settings['default'];
427+
}
428+
}
429+
}
430+
411431
private function _warn($message) {
412432
trigger_error('[' . __CLASS__ .'] ' . $message, E_USER_WARNING);
413433
}
@@ -439,7 +459,7 @@ private function _parseOption($option) {
439459
if ($this->_lexer->end() || !$this->_lexer->peek->isValue) {
440460
$optionSettings = $this->getOption($option->key);
441461

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

tests/test-arguments.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function testAddOptions()
146146
}
147147

148148
/**
149-
* Data provider with valid fags and options
149+
* Data provider with valid args and options
150150
*
151151
* @return array set of args and expected parsed values
152152
*/
@@ -206,6 +206,16 @@ public function settingsWithMissingOptionsWithDefault()
206206
);
207207
}
208208

209+
public function settingsWithNoOptionsWithDefault()
210+
{
211+
return array(
212+
array(
213+
array(),
214+
array('flag1' => false, 'flag2' => false, 'option2' => 'some default value')
215+
)
216+
);
217+
}
218+
209219
/**
210220
* Generic private testParse method.
211221
*
@@ -221,7 +231,7 @@ private function _testParse($cliParams, $expectedValues)
221231

222232
foreach ($expectedValues as $name => $value) {
223233
if ($args->isFlag($name)) {
224-
$this->assertTrue($args[$name]);
234+
$this->assertEquals($value, $args[$name]);
225235
}
226236

227237
if ($args->isOption($name)) {
@@ -262,4 +272,13 @@ public function testParseWithMissingOptionsWithDefault($cliParams, $expectedValu
262272
{
263273
$this->_testParse($cliParams, $expectedValues);
264274
}
275+
276+
/**
277+
* @param array $args arguments as they appear in the cli
278+
* @param array $expectedValues expected values after parsing
279+
* @dataProvider settingsWithNoOptionsWithDefault
280+
*/
281+
public function testParseWithNoOptionsWithDefault($cliParams, $expectedValues) {
282+
$this->_testParse($cliParams, $expectedValues);
283+
}
265284
}

0 commit comments

Comments
 (0)