Skip to content

Commit

Permalink
[Console] Fix autocompletion of argument with default value
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 20, 2021
1 parent 37d3dec commit a2c6b7c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Completion/CompletionInput.php
Expand Up @@ -109,12 +109,12 @@ public function bind(InputDefinition $definition): void
// complete argument value
$this->completionType = self::TYPE_ARGUMENT_VALUE;

$arguments = $this->getArguments();
foreach ($arguments as $argumentName => $argumentValue) {
if (null === $argumentValue) {
foreach ($this->definition->getArguments() as $argumentName => $argument) {
if (!isset($this->arguments[$argumentName])) {
break;
}

$argumentValue = $this->arguments[$argumentName];
$this->completionName = $argumentName;
if (\is_array($argumentValue)) {
$this->completionValue = $argumentValue ? $argumentValue[array_key_last($argumentValue)] : null;
Expand All @@ -124,7 +124,7 @@ public function bind(InputDefinition $definition): void
}

if ($this->currentIndex >= \count($this->tokens)) {
if (null === $arguments[$argumentName] || $this->definition->getArgument($argumentName)->isArray()) {
if (!isset($this->arguments[$argumentName]) || $this->definition->getArgument($argumentName)->isArray()) {
$this->completionName = $argumentName;
$this->completionValue = '';
} else {
Expand Down
2 changes: 1 addition & 1 deletion Tests/Command/HelpCommandTest.php
Expand Up @@ -92,7 +92,7 @@ public function provideCompletionSuggestions()

yield 'nothing' => [
[''],
[],
['completion', 'help', 'list', 'foo:bar'],
];

yield 'command_name' => [
Expand Down
14 changes: 14 additions & 0 deletions Tests/Completion/CompletionInputTest.php
Expand Up @@ -97,6 +97,20 @@ public function provideBindWithLastArrayArgumentData()
yield [CompletionInput::fromTokens(['bin/console', 'symfony', 'sen'], 2), 'sen'];
}

public function testBindArgumentWithDefault()
{
$definition = new InputDefinition([
new InputArgument('arg-with-default', InputArgument::OPTIONAL, '', 'default'),
]);

$input = CompletionInput::fromTokens(['bin/console'], 1);
$input->bind($definition);

$this->assertEquals(CompletionInput::TYPE_ARGUMENT_VALUE, $input->getCompletionType(), 'Unexpected type');
$this->assertEquals('arg-with-default', $input->getCompletionName(), 'Unexpected name');
$this->assertEquals('', $input->getCompletionValue(), 'Unexpected value');
}

/**
* @dataProvider provideFromStringData
*/
Expand Down

0 comments on commit a2c6b7c

Please sign in to comment.