Skip to content

Commit

Permalink
Merge pull request #19 from stratease/fix-boolean
Browse files Browse the repository at this point in the history
fixed a typo in unit test, and made boolean options default to false ins...
  • Loading branch information
nategood committed Feb 2, 2014
2 parents 2a3444a + c9eab95 commit 93a8d97
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Commando/Command.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function parse()

$option = $this->getOption($name);
if ($option->isBoolean()) {
$keyvals[$name] = true;
$keyvals[$name] = !$option->getDefault();// inverse of the default, as expected
} else {
// the next token MUST be an "argument" and not another flag/option
$token = array_shift($tokens);
Expand All @@ -353,9 +353,9 @@ public function parse()
}
}
}

// Set values (validates and performs map when applicable)
foreach ($keyvals as $key => $value) {

$this->getOption($key)->setValue($value);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Commando/Option.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function setDescription($description)
*/
public function setBoolean($bool = true)
{
// if we didn't define a default already, set false as the default value...
if($this->default === null) {
$this->setDefault(false);
}
$this->boolean = $bool;
return $this;
}
Expand Down Expand Up @@ -126,6 +130,14 @@ public function setDefault($value)
return $this;
}

/**
* @return mixed
*/
public function getDefault()
{
return $this->default;
}

/**
* @param closure|string $rule regex, closure
* @return Option
Expand Down
36 changes: 34 additions & 2 deletions tests/Commando/CommandTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testImplicitAndExplicitParse()
}

// Test retrieving a previously defined option via option($name)
public function testRevtrievingOptionNamed()
public function testRetrievingOptionNamed()
{
// Short flag
$tokens = array('filename', '-f', 'val');
Expand All @@ -94,7 +94,7 @@ public function testRevtrievingOptionNamed()
}

// Test retrieving a previously defined option via option($name)
public function testRevtrievingOptionAnon()
public function testRetrievingOptionAnon()
{
// Annonymous
$tokens = array('filename', 'arg1', 'arg2', 'arg3');
Expand All @@ -108,6 +108,38 @@ public function testRevtrievingOptionAnon()
$this->assertEquals(1, $cmd->getSize());
}

public function testBooleanOption()
{
// with bool flag
$tokens = array('filename', 'arg1', '-b', 'arg2');
$cmd = new Command($tokens);
$cmd->option('b')
->boolean();
$this->assertTrue($cmd['b']);
// without
$tokens = array('filename', 'arg1', 'arg2');
$cmd = new Command($tokens);
$cmd->option('b')
->boolean();
$this->assertFalse($cmd['b']);

// try inverse bool default operations...
// with bool flag
$tokens = array('filename', 'arg1', '-b', 'arg2');
$cmd = new Command($tokens);
$cmd->option('b')
->default(true)
->boolean();
$this->assertFalse($cmd['b']);
// without
$tokens = array('filename', 'arg1', 'arg2');
$cmd = new Command($tokens);
$cmd->option('b')
->default(true)
->boolean();
$this->assertTrue($cmd['b']);
}

public function testGetValues()
{
$tokens = array('filename', '-a', 'v1', '-b', 'v2', 'v3', 'v4', 'v5');
Expand Down

0 comments on commit 93a8d97

Please sign in to comment.