Skip to content

[Console] Document completion changes for 6.1 #16724

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 1 commit into from
May 1, 2022
Merged
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
4 changes: 4 additions & 0 deletions console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 29 additions & 18 deletions console/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion page_creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <console-completion-setup>` for more
information on how to set up completion.
Expand Down