Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed a typo in unit test, and made boolean options default to false ins... #19

Merged
merged 4 commits into from
Feb 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This makes total sense, but I can imagine this potentially causing headaches if the developer isn't fully aware of this. Feels a bit awkward. Let me think on this. Totally open to hearing an argument for this case.

} 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