merged branch jmikola/2.2-fix/gh7689 (PR #7690)
This PR was submitted for the 2.2 branch but it was merged into the 2.1 branch instead (closes #7690). Discussion ---------- [Console] Fix default value handling for multi-value options | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7689 | License | MIT The default value for array options will be an array, so it is not suitable to use as the default when processing one of many values for a multi-value option. Using null seems appropriate here, as it indicates the absence of a value and also converts nicely to an empty string (as opposed to an empty array). Commits ------- a9c28ff [Console] Fix default value handling for multi-value options
This commit is contained in:
commit
41fef9310a
@ -215,6 +215,11 @@ class ArgvInput extends Input
|
|||||||
|
|
||||||
$option = $this->definition->getOption($name);
|
$option = $this->definition->getOption($name);
|
||||||
|
|
||||||
|
// Convert false values (from a previous call to substr()) to null
|
||||||
|
if (false === $value) {
|
||||||
|
$value = null;
|
||||||
|
}
|
||||||
|
|
||||||
if (null === $value && $option->acceptValue() && count($this->parsed)) {
|
if (null === $value && $option->acceptValue() && count($this->parsed)) {
|
||||||
// if option accepts an optional or mandatory argument
|
// if option accepts an optional or mandatory argument
|
||||||
// let's see if there is one provided
|
// let's see if there is one provided
|
||||||
@ -233,7 +238,9 @@ class ArgvInput extends Input
|
|||||||
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
|
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $option->isValueOptional() ? $option->getDefault() : true;
|
if (!$option->isArray()) {
|
||||||
|
$value = $option->isValueOptional() ? $option->getDefault() : true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($option->isArray()) {
|
if ($option->isArray()) {
|
||||||
|
@ -162,7 +162,22 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
|
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
|
||||||
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
|
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
|
||||||
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
|
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
|
||||||
|
|
||||||
|
$input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz'));
|
||||||
|
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
|
||||||
|
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
|
||||||
|
|
||||||
|
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
|
||||||
|
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
|
||||||
|
$this->assertSame(array('name' => array('foo', 'bar', null)), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
|
||||||
|
|
||||||
|
$input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
|
||||||
|
$input->bind(new InputDefinition(array(
|
||||||
|
new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
|
||||||
|
new InputOption('anotherOption', null, InputOption::VALUE_NONE),
|
||||||
|
)));
|
||||||
|
$this->assertSame(array('name' => array('foo', 'bar', null), 'anotherOption' => true), $input->getOptions(), '->parse() parses empty array options as null ("--option value" syntax)');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$input = new ArgvInput(array('cli.php', '-1'));
|
$input = new ArgvInput(array('cli.php', '-1'));
|
||||||
|
Reference in New Issue
Block a user