Skip to content

Commit 7d2e068

Browse files
committed
bug #41535 [Console] Fix negated options not accessible (jderusse)
This PR was merged into the 5.3 branch. Discussion ---------- [Console] Fix negated options not accessible | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #41531 | License | MIT | Doc PR | - Removing the `--no-ansi` option to only let the negatable option `--ansi` breaks applications that expects calling `$input->getOption('no-ansi')` This PR provides a fallback to return the negative value of the negatable options Commits ------- 440bd5f1c7 [Console] Fix negated options not accessible
2 parents dca646d + c9824f0 commit 7d2e068

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Input/Input.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ public function getOptions()
146146
*/
147147
public function getOption(string $name)
148148
{
149+
if ($this->definition->hasNegation($name)) {
150+
if (null === $value = $this->getOption($this->definition->negationToName($name))) {
151+
return $value;
152+
}
153+
154+
return !$value;
155+
}
156+
149157
if (!$this->definition->hasOption($name)) {
150158
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
151159
}
@@ -158,7 +166,11 @@ public function getOption(string $name)
158166
*/
159167
public function setOption(string $name, $value)
160168
{
161-
if (!$this->definition->hasOption($name)) {
169+
if ($this->definition->hasNegation($name)) {
170+
$this->options[$this->definition->negationToName($name)] = !$value;
171+
172+
return;
173+
} elseif (!$this->definition->hasOption($name)) {
162174
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
163175
}
164176

@@ -170,7 +182,7 @@ public function setOption(string $name, $value)
170182
*/
171183
public function hasOption(string $name)
172184
{
173-
return $this->definition->hasOption($name);
185+
return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
174186
}
175187

176188
/**

Tests/Input/InputTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public function testOptions()
4545
$input = new ArrayInput(['--name' => 'foo', '--bar' => null], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
4646
$this->assertNull($input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
4747
$this->assertEquals(['name' => 'foo', 'bar' => null], $input->getOptions(), '->getOptions() returns all option values');
48+
49+
$input = new ArrayInput(['--name' => null], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
50+
$this->assertTrue($input->hasOption('name'));
51+
$this->assertTrue($input->hasOption('no-name'));
52+
$this->assertTrue($input->getOption('name'));
53+
$this->assertFalse($input->getOption('no-name'));
54+
55+
$input = new ArrayInput(['--no-name' => null], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
56+
$this->assertFalse($input->getOption('name'));
57+
$this->assertTrue($input->getOption('no-name'));
58+
59+
$input = new ArrayInput([], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
60+
$this->assertNull($input->getOption('name'));
61+
$this->assertNull($input->getOption('no-name'));
4862
}
4963

5064
public function testSetInvalidOption()

0 commit comments

Comments
 (0)