@@ -313,7 +313,7 @@ can also implement value completion for the input in your commands. For
313313instance, you may want to complete all usernames from the database in the
314314``name `` argument of your greet command.
315315
316- To achieve this, override the `` complete () `` method in the command ::
316+ To achieve this, use the 5th argument of `` addArgument () ``/`` addOption `` ::
317317
318318 // ...
319319 use Symfony\Component\Console\Completion\CompletionInput;
@@ -322,32 +322,43 @@ To achieve this, override the ``complete()`` method in the command::
322322 class GreetCommand extends Command
323323 {
324324 // ...
325-
326- public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
325+ protected function configure(): void
327326 {
328- if ($input->mustSuggestArgumentValuesFor('names')) {
329- // the user asks for completion input for the "names" option
330-
331- // the value the user already typed, e.g. when typing "app:greet Fa" before
332- // pressing Tab, this will contain "Fa"
333- $currentValue = $input->getCompletionValue();
334-
335- // get the list of username names from somewhere (e.g. the database)
336- // you may use $currentValue to filter down the names
337- $availableUsernames = ...;
338-
339- // then add the retrieved names as suggested values
340- $suggestions->suggestValues($availableUsernames);
341- }
327+ $this
328+ ->addArgument(
329+ 'names',
330+ InputArgument::IS_ARRAY,
331+ 'Who do you want to greet (separate multiple names with a space)?',
332+ null,
333+ function (CompletionInput $input) {
334+ // the value the user already typed, e.g. when typing "app:greet Fa" before
335+ // pressing Tab, this will contain "Fa"
336+ $currentValue = $input->getCompletionValue();
337+
338+ // get the list of username names from somewhere (e.g. the database)
339+ // you may use $currentValue to filter down the names
340+ $availableUsernames = ...;
341+
342+ // then suggested the usernames as values
343+ return $availableUsernames;
344+ }
345+ )
346+ ;
342347 }
343348 }
344349
350+ .. versionadded :: 6.1
351+
352+ The argument to ``addOption() ``/``addArgument() `` was introduced in
353+ Symfony 6.1. Prior to this version, you had to override the
354+ ``complete() `` method of the command.
355+
345356That's all you need! Assuming users "Fabien" and "Fabrice" exist, pressing
346357tab after typing ``app:greet Fa `` will give you these names as a suggestion.
347358
348359.. tip ::
349360
350- The bash shell is able to handle huge amounts of suggestions and will
361+ The shell script is able to handle huge amounts of suggestions and will
351362 automatically filter the suggested values based on the existing input
352363 from the user. You do not have to implement any filter logic in the
353364 command.
0 commit comments