@@ -317,7 +317,7 @@ can also implement value completion for the input in your commands. For
317
317
instance, you may want to complete all usernames from the database in the
318
318
``name `` argument of your greet command.
319
319
320
- To achieve this, override the `` complete () `` method in the command ::
320
+ To achieve this, use the 5th argument of `` addArgument () ``/`` addOption `` ::
321
321
322
322
// ...
323
323
use Symfony\Component\Console\Completion\CompletionInput;
@@ -326,32 +326,43 @@ To achieve this, override the ``complete()`` method in the command::
326
326
class GreetCommand extends Command
327
327
{
328
328
// ...
329
-
330
- public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
329
+ protected function configure(): void
331
330
{
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
+ ;
346
351
}
347
352
}
348
353
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
+
349
360
That's all you need! Assuming users "Fabien" and "Fabrice" exist, pressing
350
361
tab after typing ``app:greet Fa `` will give you these names as a suggestion.
351
362
352
363
.. tip ::
353
364
354
- The bash shell is able to handle huge amounts of suggestions and will
365
+ The shell script is able to handle huge amounts of suggestions and will
355
366
automatically filter the suggested values based on the existing input
356
367
from the user. You do not have to implement any filter logic in the
357
368
command.
0 commit comments