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