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

Skip to content

[Console] Fix allow passing a default argument when InputOption::IS_NEGATABLE is used #52058

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

Open
wants to merge 3 commits into
base: 6.4
Choose a base branch
from
Open
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/Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function setDefault(string|bool|int|float|array|null $default = null)
if (1 > \func_num_args()) {
trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
}
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && !$this->isNegatable() && null !== $default) {
throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public function testSetDefault()
$option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
$option->setDefault([1, 2]);
$this->assertEquals([1, 2], $option->getDefault(), '->setDefault() changes the default value');

$option = new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE);
$option->setDefault(true);
$this->assertTrue($option->getDefault(), '->setDefault() changes the default value');
}

public function testDefaultValueWithValueNoneMode()
Expand Down
Loading