Skip to content

Commit

Permalink
feature #16724 [Console] Document completion changes for 6.1 (wouterj)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.1 branch.

Discussion
----------

[Console] Document completion changes for 6.1

Fixes #16623 and fixes #16231
Includes #16723 (I'll rebase after that one is merged)

Commits
-------

1878694 Document input definition completion and Fish support
  • Loading branch information
wouterj committed May 1, 2022
2 parents d2f1536 + 1878694 commit 6be527e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
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

0 comments on commit 6be527e

Please sign in to comment.