Skip to content

[Console] Both ansi and no-ansi return false by default #41794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ protected function getDefaultInputDefinition()
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', false),
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', null),
new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
]);
}
Expand Down
16 changes: 13 additions & 3 deletions src/Symfony/Component/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ public function getOptions()
public function getOption(string $name)
{
if ($this->definition->hasNegation($name)) {
if (null === $value = $this->getOption($this->definition->negationToName($name))) {
return $value;
$negatedName = $this->definition->negationToName($name);
if (!$this->definition->hasOption($negatedName)) {
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $negatedName));
}
$value = \array_key_exists($negatedName, $this->options) ? $this->options[$negatedName] : $this->definition->getOption($negatedName)->getDefault();
if (null === $value) {
return false;
}

return !$value;
Expand All @@ -158,7 +163,12 @@ public function getOption(string $name)
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
}

return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
$value = \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
if (null === $value && $this->definition->getOption($name)->isNegatable()) {
return false;
}

return $value;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Tests/Input/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function testOptions()
$this->assertTrue($input->getOption('no-name'));

$input = new ArrayInput([], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
$this->assertNull($input->getOption('name'));
$this->assertNull($input->getOption('no-name'));
$this->assertFalse($input->getOption('name'));
$this->assertFalse($input->getOption('no-name'));
}

public function testSetInvalidOption()
Expand Down