Skip to content
Permalink
Browse files

Add support for multiple values option in the Shell system

  • Loading branch information...
HavokInspiration committed Jan 2, 2017
1 parent 2df03ff commit d94a39f52377a626544d09097eceb9ff5a7b8582
@@ -61,6 +61,13 @@ class ConsoleInputOption
*/
protected $_default;
/**
* Can the option accept multiple value definition.
*
* @var bool
*/
protected $_multiple;
/**
* An array of choices for the option.
*
@@ -77,9 +84,18 @@ class ConsoleInputOption
* @param bool $boolean Whether this option is a boolean option. Boolean options don't consume extra tokens
* @param string $default The default value for this option.
* @param array $choices Valid choices for this option.
* @param bool $multiple Whether this option can accept multiple value definition.
* @throws \Cake\Console\Exception\ConsoleException
*/
public function __construct($name, $short = '', $help = '', $boolean = false, $default = '', $choices = [])
public function __construct(
$name,
$short = '',
$help = '',
$boolean = false,
$default = '',
$choices = [],
$multiple = false
)
{
if (is_array($name) && isset($name['name'])) {
foreach ($name as $key => $value) {
@@ -92,6 +108,7 @@ public function __construct($name, $short = '', $help = '', $boolean = false, $d
$this->_boolean = $boolean;
$this->_default = $default;
$this->_choices = $choices;
$this->_multiple = $multiple;
}
if (strlen($this->_short) > 1) {
throw new ConsoleException(
@@ -185,6 +202,16 @@ public function isBoolean()
return (bool)$this->_boolean;
}
/**
* Check if this option accepts multiple values.
*
* @return bool
*/
public function acceptsMultiple()
{
return (bool)$this->_multiple;
}
/**
* Check that a value is a valid choice for this option.
*
@@ -736,7 +736,11 @@ protected function _parseOption($name, $params)
$value = $option->defaultValue();
}
if ($option->validChoice($value)) {
$params[$name] = $value;
if ($option->acceptsMultiple($value)) {
$params[$name][] = $value;
} else {
$params[$name] = $value;
}
return $params;
}
@@ -180,6 +180,29 @@ public function testAddOptionShort()
$this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Short parameter did not parse out');
}
/**
* test adding an option and using the short value for parsing.
*
* @return void
*/
public function testAddOptionWithMultipleShort()
{
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('source', [
'multiple' => true,
'short' => 's'
]);
$result = $parser->parse(['-s', 'mysql', '-s', 'postgres']);
$this->assertEquals(
[
'source' => ['mysql', 'postgres'],
'help' => false
],
$result[0],
'Short parameter did not parse out'
);
}
/**
* Test that adding an option using a two letter short value causes an exception.
* As they will not parse correctly.
@@ -251,6 +274,53 @@ public function testMultipleOptions()
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
}
/**
* test adding an option that accepts multiple values.
*
* @return void
*/
public function testAddOptionWithMultiple()
{
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('source', ['multiple' => true]);
$result = $parser->parse(['--source', 'mysql', '--source', 'postgres']);
$expected = [
'source' => [
'mysql',
'postgres'
],
'help' => false
];
$this->assertEquals($expected, $result[0], 'options with multiple values did not parse');
}
/**
* test adding multiple options, including one that accepts multiple values.
*
* @return void
*/
public function testAddMultipleOptionsWithMultiple()
{
$parser = new ConsoleOptionParser('test', false);
$parser
->addOption('source', ['multiple' => true])
->addOption('name')
->addOption('export', ['boolean' => true]);
$result = $parser->parse(['--export', '--source', 'mysql', '--name', 'annual-report', '--source', 'postgres']);
$expected = [
'export' => true,
'source' => [
'mysql',
'postgres'
],
'name' => 'annual-report',
'help' => false
];
$this->assertEquals($expected, $result[0], 'options with multiple values did not parse');
}
/**
* Test adding multiple options.
*

0 comments on commit d94a39f

Please sign in to comment.
You can’t perform that action at this time.