Skip to content

[Console] Negatable option are null by default #40986

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

Merged
merged 1 commit into from
Apr 29, 2021
Merged
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 @@ -1036,7 +1036,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', null),
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', false),
new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
]);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/Console/Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ public function setDefault($default = null)
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
}
if (self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode) && null !== $default) {
throw new LogicException('Cannot set a default value when using InputOption::VALUE_NEGATABLE mode.');
}

if ($this->isArray()) {
if (null === $default) {
Expand All @@ -198,7 +195,7 @@ public function setDefault($default = null)
}
}

$this->default = $this->acceptValue() ? $default : false;
$this->default = $this->acceptValue() || $this->isNegatable() ? $default : false;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ public function provideNegatableOptions()
['foo' => false],
'->parse() parses long options without a value',
],
[
['cli.php'],
[new InputOption('foo', null, InputOption::VALUE_NEGATABLE)],
['foo' => null],
'->parse() parses long options without a value',
],
[
['cli.php'],
[new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)],
['foo' => null],
'->parse() parses long options without a value',
],
[
['cli.php'],
[new InputOption('foo', null, InputOption::VALUE_NEGATABLE, '', false)],
['foo' => false],
'->parse() parses long options without a value',
],
];
}

Expand Down
8 changes: 0 additions & 8 deletions src/Symfony/Component/Console/Tests/Input/InputOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,6 @@ public function testDefaultValueWithValueNoneMode()
$option->setDefault('default');
}

public function testDefaultValueWithValueBooleanMode()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot set a default value when using InputOption::VALUE_NEGATABLE mode.');
$option = new InputOption('foo', 'f', InputOption::VALUE_NEGATABLE);
$option->setDefault('default');
}

public function testDefaultValueWithIsArrayMode()
{
$this->expectException(\LogicException::class);
Expand Down