Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41fef93

Browse files
committed
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
2 parents 324686c + 5abf887 commit 41fef93

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ private function addLongOption($name, $value)
215215

216216
$option = $this->definition->getOption($name);
217217

218+
// Convert false values (from a previous call to substr()) to null
219+
if (false === $value) {
220+
$value = null;
221+
}
222+
218223
if (null === $value && $option->acceptValue() && count($this->parsed)) {
219224
// if option accepts an optional or mandatory argument
220225
// let's see if there is one provided
@@ -233,7 +238,9 @@ private function addLongOption($name, $value)
233238
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
234239
}
235240

236-
$value = $option->isValueOptional() ? $option->getDefault() : true;
241+
if (!$option->isArray()) {
242+
$value = $option->isValueOptional() ? $option->getDefault() : true;
243+
}
237244
}
238245

239246
if ($option->isArray()) {

src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,22 @@ public function testParser()
162162

163163
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
164164
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
165-
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
165+
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
166+
167+
$input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz'));
168+
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
169+
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
170+
171+
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
172+
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
173+
$this->assertSame(array('name' => array('foo', 'bar', null)), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
174+
175+
$input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
176+
$input->bind(new InputDefinition(array(
177+
new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
178+
new InputOption('anotherOption', null, InputOption::VALUE_NONE),
179+
)));
180+
$this->assertSame(array('name' => array('foo', 'bar', null), 'anotherOption' => true), $input->getOptions(), '->parse() parses empty array options as null ("--option value" syntax)');
166181

167182
try {
168183
$input = new ArgvInput(array('cli.php', '-1'));

0 commit comments

Comments
 (0)