From 18786941f312ab811748e4177baf9c770083e4b3 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sat, 16 Apr 2022 17:09:48 +0200 Subject: [PATCH] Document input definition completion and Fish support --- console.rst | 4 ++++ console/input.rst | 47 +++++++++++++++++++++++++++++------------------ page_creation.rst | 2 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/console.rst b/console.rst index 6969bd75e1a..20476e88206 100644 --- a/console.rst +++ b/console.rst @@ -59,6 +59,10 @@ command, for instance: Console Completion ~~~~~~~~~~~~~~~~~~ +.. versionadded:: 6.1 + + Console completion for Fish was introduced in Symfony 6.1. + If you are using the Bash shell, you can install Symfony's completion script to get auto completion when typing commands in the terminal. All commands support name and option completion, and some can even complete diff --git a/console/input.rst b/console/input.rst index 36718199e6f..2182be5409c 100644 --- a/console/input.rst +++ b/console/input.rst @@ -313,7 +313,7 @@ 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:: +To achieve this, use the 5th argument of ``addArgument()``/``addOption``:: // ... use Symfony\Component\Console\Completion\CompletionInput; @@ -322,32 +322,43 @@ To achieve this, override the ``complete()`` method in the command:: class GreetCommand extends Command { // ... - - public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + protected function configure(): 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); - } + $this + ->addArgument( + 'names', + InputArgument::IS_ARRAY, + 'Who do you want to greet (separate multiple names with a space)?', + null, + function (CompletionInput $input) { + // 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 suggested the usernames as values + return $availableUsernames; + } + ) + ; } } +.. versionadded:: 6.1 + + The argument to ``addOption()``/``addArgument()`` was introduced in + Symfony 6.1. Prior to this version, you had to override the + ``complete()`` method of the command. + 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 + The shell script 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. diff --git a/page_creation.rst b/page_creation.rst index af315b188c4..b9d6b6e43b5 100644 --- a/page_creation.rst +++ b/page_creation.rst @@ -197,7 +197,7 @@ You'll learn about many more commands as you continue! .. tip:: - If you are using the Bash shell, you can set up completion support. + If you are using the Bash or Fish shell, you can set up completion support. This autocompletes commands and other input when using ``bin/console``. See :ref:`the Console document ` for more information on how to set up completion.