Skip to content

Commit 96cb132

Browse files
committed
Document input definition completion and Fish support
1 parent 70a0ab5 commit 96cb132

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

console.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ command, for instance:
5959
Console Completion
6060
~~~~~~~~~~~~~~~~~~
6161

62-
.. versionadded:: 5.4
62+
.. versionadded:: 6.1
6363

64-
Console completion for Bash was introduced in Symfony 5.4.
64+
Console completion for Fish was introduced in Symfony 6.1.
6565

6666
If you are using the Bash shell, you can install Symfony's completion
6767
script to get auto completion when typing commands in the terminal. All

console/input.rst

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ can also implement value completion for the input in your commands. For
317317
instance, you may want to complete all usernames from the database in the
318318
``name`` argument of your greet command.
319319

320-
To achieve this, override the ``complete()`` method in the command::
320+
To achieve this, use the 5th argument of ``addArgument()``/``addOption``::
321321

322322
// ...
323323
use Symfony\Component\Console\Completion\CompletionInput;
@@ -326,32 +326,43 @@ To achieve this, override the ``complete()`` method in the command::
326326
class GreetCommand extends Command
327327
{
328328
// ...
329-
330-
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
329+
protected function configure(): void
331330
{
332-
if ($input->mustSuggestArgumentValuesFor('names')) {
333-
// the user asks for completion input for the "names" option
334-
335-
// the value the user already typed, e.g. when typing "app:greet Fa" before
336-
// pressing Tab, this will contain "Fa"
337-
$currentValue = $input->getCompletionValue();
338-
339-
// get the list of username names from somewhere (e.g. the database)
340-
// you may use $currentValue to filter down the names
341-
$availableUsernames = ...;
342-
343-
// then add the retrieved names as suggested values
344-
$suggestions->suggestValues($availableUsernames);
345-
}
331+
$this
332+
->addArgument(
333+
'names',
334+
InputArgument::IS_ARRAY,
335+
'Who do you want to greet (separate multiple names with a space)?',
336+
null,
337+
function (CompletionInput $input) {
338+
// the value the user already typed, e.g. when typing "app:greet Fa" before
339+
// pressing Tab, this will contain "Fa"
340+
$currentValue = $input->getCompletionValue();
341+
342+
// get the list of username names from somewhere (e.g. the database)
343+
// you may use $currentValue to filter down the names
344+
$availableUsernames = ...;
345+
346+
// then suggested the usernames as values
347+
return $availableUsernames;
348+
}
349+
)
350+
;
346351
}
347352
}
348353

354+
.. versionadded:: 6.1
355+
356+
The argument to ``addOption()``/``addArgument()`` was introduced in
357+
Symfony 6.1. Prior to this version, you had to override the
358+
``complete()`` method of the command.
359+
349360
That's all you need! Assuming users "Fabien" and "Fabrice" exist, pressing
350361
tab after typing ``app:greet Fa`` will give you these names as a suggestion.
351362

352363
.. tip::
353364

354-
The bash script will automatically filter the suggested values based on
365+
The shell script will automatically filter the suggested values based on
355366
the existing input from the user. You do not have to implement any
356367
filter logic in the command.
357368

page_creation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ You'll learn about many more commands as you continue!
197197

198198
.. tip::
199199

200-
If you are using the Bash shell, you can set-up completion support.
200+
If you are using the Bash or Fish shell, you can set-up completion support.
201201
This autocompletes commands and other input when using ``bin/console``.
202202
See :ref:`the Console document <console-completion-setup>` for more
203203
information on how to set-up completion.

0 commit comments

Comments
 (0)