Skip to content

Commit

Permalink
Allows arguments to be set definitely using $this->args()->set() in…
Browse files Browse the repository at this point in the history
… kahlan config files.
  • Loading branch information
jails committed Feb 11, 2015
1 parent 5665d07 commit 03c910c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
18 changes: 18 additions & 0 deletions spec/suite/cli/ArgsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,24 @@

});

context("with override set to `false`", function() {

it("doesn't override existing arguments when the override params is set to `false`", function() {

$args = new Args();
$args->set('argument1', 'value1');
$actual = $args->parse(['--argument1=valueX']);
expect($actual)->toBe(['argument1' => 'valueX']);

$args = new Args();
$args->set('argument1', 'value1');
$actual = $args->parse(['--argument1=valueX'], false);
expect($actual)->toBe(['argument1' => 'value1']);

});

});

});

describe("->get()", function() {
Expand Down
13 changes: 13 additions & 0 deletions spec/suite/cli/KahlanSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@

});

it("doesn't filter empty string from include & exclude", function() {

$args = [
'--include=',
'--exclude=',
];

$this->specs->loadConfig($args);
expect($this->specs->args()->get()['include'])->toBe([]);
expect($this->specs->args()->get()['exclude'])->toBe([]);

});

});

describe("->run()", function() {
Expand Down
15 changes: 10 additions & 5 deletions src/cli/Args.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,24 @@ public function argument($name = null, $config = [], $value = null)
/**
* Parses a command line argv.
*
* @param array $argv An argv data.
* @return array The parsed attributes
* @param array $argv An argv data.
* @param boolean $override If set to `false` it doesn't override already setted data.
* @return array The parsed attributes
*/
public function parse($argv)
public function parse($argv, $override = true)
{
$this->_values = [];
$exists = [];
$override ? $this->_values = [] : $exists = array_fill_keys(array_keys($this->_values), true);

foreach($argv as $arg) {
if ($arg === '--') {
break;
}
if ($arg[0] === '-') {
list($name, $value) = $this->_parse(ltrim($arg,'-'));
$this->add($name, $value);
if ($override || !isset($exists[$name])) {
$this->add($name, $value, $override);
}
}
}
return $this->get();
Expand Down
18 changes: 15 additions & 3 deletions src/cli/Kahlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,20 @@ public function __construct($options = [])
$args->argument('config', ['default' => 'kahlan-config.php']);
$args->argument('ff', ['type' => 'numeric', 'default' => 0]);
$args->argument('no-colors', ['type' => 'boolean', 'default' => false]);
$args->argument('include', ['array' => 'true', 'default' => ['*']]);
$args->argument('exclude', ['array' => 'true', 'default' => []]);
$args->argument('include', [
'array' => 'true',
'default' => ['*'],
'value' => function($value) {
return array_filter($value);
}
]);
$args->argument('exclude', [
'array' => 'true',
'default' => [],
'value' => function($value) {
return array_filter($value);
}
]);
$args->argument('persistent', ['type' => 'boolean', 'default' => true]);
$args->argument('autoclear', ['array' => 'true', 'default' => [
'kahlan\plugin\Monkey',
Expand Down Expand Up @@ -162,7 +174,7 @@ public function loadConfig($argv = [])
require $args->get('config');
}

$this->_args->parse($argv);
$this->_args->parse($argv, false);
}

/**
Expand Down

0 comments on commit 03c910c

Please sign in to comment.