8000 bug #46341 Fix aliases handling in command name completion (Seldaek) · symfony/symfony@2d44f22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d44f22

Browse files
committed
bug #46341 Fix aliases handling in command name completion (Seldaek)
This PR was merged into the 5.4 branch. Discussion ---------- Fix aliases handling in command name completion | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> While working on composer/composer#10320 I noticed that command aliases like `composer why` did not autocomplete and then even if typed manually the args/options did not autocomplete if using the alias. This PR fixes it. Commits ------- 8ffc015 Fix aliases handling in command name completion
2 parents 5584221 + 8ffc015 commit 2d44f22

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,18 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
363363
CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType()
364364
&& 'command' === $input->getCompletionName()
365365
) {
366-
$suggestions->suggestValues(array_filter(array_map(function (Command $command) {
367-
return $command->isHidden() ? null : $command->getName();
368-
}, $this->all())));
366+
$commandNames = [];
367+
foreach ($this->all() as $name => $command) {
368+
// skip hidden commands and aliased commands as they already get added below
369+
if ($command->isHidden() || $command->getName() !== $name) {
370+
continue;
371+
}
372+
$commandNames[] = $command->getName();
373+
foreach ($command->getAliases() as $name) {
374+
$commandNames[] = $name;
375+
}
376+
}
377+
$suggestions->suggestValues(array_filter($commandNames));
369378

370379
return;
371380
}

src/Symfony/Component/Console/Command/CompleteCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105105
} elseif (
106106
$completionInput->mustSuggestArgumentValuesFor('command')
107107
&& $command->getName() !== $completionInput->getCompletionValue()
108+
&& !\in_array($completionInput->getCompletionValue(), $command->getAliases(), true)
108109
) {
109110
$this->log(' No command found, completing using the Application class.');
110111

111112
// expand shortcut names ("cache:cl<TAB>") into their full name ("cache:clear")
112-
$suggestions->suggestValue($command->getName());
113+
$suggestions->suggestValues(array_filter(array_merge([$command->getName()], $command->getAliases())));
113114
} else {
114115
$command->mergeApplicationDefinition();
115116
$completionInput->bind($command->getDefinition());

src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ public function testCompleteCommandName(array $input, array $suggestions)
102102

103103
public function provideCompleteCommandNameInputs()
104104
{
105-
yield 'empty' => [['bin/console'], ['help', 'list', 'completion', 'hello']];
106-
yield 'partial' => [['bin/console', 'he'], ['help', 'list', 'completion', 'hello']];
107-
yield 'complete-shortcut-name' => [['bin/console', 'hell'], ['hello']];
105+
yield 'empty' => [['bin/console'], ['help', 'list', 'completion', 'hello', 'ahoy']];
106+
yield 'partial' => [['bin/console', 'he'], ['help', 'list', 'completion', 'hello', 'ahoy']];
107+
yield 'complete-shortcut-name' => [['bin/console', 'hell'], ['hello', 'ahoy']];
108+
yield 'complete-aliases' => [['bin/console', 'ah'], ['hello', 'ahoy']];
108109
}
109110

110111
/**
@@ -120,6 +121,8 @@ public function provideCompleteCommandInputDefinitionInputs()
120121
{
121122
yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-interaction']];
122123
yield 'custom' => [['bin/console', 'hello'], ['Fabien', 'Robin', 'Wouter']];
124+
yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-interaction']];
125+
yield 'custom-aliased' => [['bin/console', 'ahoy'], ['Fabien', 'Robin', 'Wouter']];
123126
}
124127

125128
private function execute(array $input)
@@ -134,6 +137,7 @@ class CompleteCommandTest_HelloCommand extends Command
134137
public function configure(): void
135138
{
136139
$this->setName('hello')
140+
->setAliases(['ahoy'])
137141
->addArgument('name', InputArgument::REQUIRED)
138142
;
139143
}

0 commit comments

Comments
 (0)
0