-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Console] Document console completion #16723
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,4 +308,91 @@ The above code can be simplified as follows because ``false !== null``:: | |
$yell = ($optionValue !== false); | ||
$yellLouder = ($optionValue === 'louder'); | ||
|
||
Adding Argument/Option Value Completion | ||
--------------------------------------- | ||
|
||
.. versionadded:: 5.4 | ||
|
||
Console completion was introduced in Symfony 5.4. | ||
|
||
If :ref:`Console completion is installed <console-completion-setup>`, | ||
command and option names will be auto completed by the shell. However, you | ||
can also implement value completion for the input in your commands. For | ||
instance, you may want to complete all usernames from the database in the | ||
``name`` argument of your greet command. | ||
|
||
To achieve this, override the ``complete()`` method in the command:: | ||
|
||
// ... | ||
use Symfony\Component\Console\Completion\CompletionInput; | ||
use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
|
||
class GreetCommand extends Command | ||
{ | ||
// ... | ||
|
||
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void | ||
{ | ||
if ($input->mustSuggestArgumentValuesFor('names')) { | ||
// the user asks for completion input for the "names" option | ||
|
||
// the value the user already typed, e.g. when typing "app:greet Fa" before | ||
// pressing Tab, this will contain "Fa" | ||
$currentValue = $input->getCompletionValue(); | ||
|
||
// get the list of username names from somewhere (e.g. the database) | ||
// you may use $currentValue to filter down the names | ||
$availableUsernames = ...; | ||
|
||
// then add the retrieved names as suggested values | ||
$suggestions->suggestValues($availableUsernames); | ||
} | ||
} | ||
} | ||
|
||
That's all you need! Assuming users "Fabien" and "Fabrice" exist, pressing | ||
tab after typing ``app:greet Fa`` will give you these names as a suggestion. | ||
|
||
.. tip:: | ||
|
||
The bash shell is able to handle huge amounts of suggestions and will | ||
automatically filter the suggested values based on the existing input | ||
from the user. You do not have to implement any filter logic in the | ||
command. | ||
|
||
You may use ``CompletionInput::getCompletionValue()`` to get the | ||
current input if that helps improving performance (e.g. by reducing the | ||
number of rows fetched from the database). | ||
|
||
Testing the Completion script | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The Console component comes with a special | ||
:class:`Symfony\\Component\\Console\\Test\\CommandCompletionTester`` class | ||
to help you unit test the completion logic:: | ||
|
||
// ... | ||
use Symfony\Component\Console\Application; | ||
|
||
class GreetCommandTest extends TestCase | ||
{ | ||
public function testComplete() | ||
{ | ||
$application = new Application(); | ||
$application->add(new GreetCommand()); | ||
|
||
// create a new tester with the greet command | ||
$tester = new CommandCompletionTester($application->get('app:greet')); | ||
|
||
// complete the input without any existing input (the empty string represents | ||
// the position of the cursor) | ||
$suggestions = $tester->complete(['']); | ||
$this->assertSame(['Fabien', 'Fabrice', 'Wouter'], $suggestions); | ||
|
||
// complete the input with "Fa" as input | ||
$suggestions = $tester->complete(['Fa']); | ||
$this->assertSame(['Fabien', 'Fabrice'], $suggestions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Users will think they need to filter the values themself, while this is done by the completion tool. |
||
} | ||
} | ||
|
||
.. _`docopt standard`: http://docopt.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.