From 5cfc67e595f1976621bc4f569b54950e5caf6d45 Mon Sep 17 00:00:00 2001 From: Mikkel Paulson Date: Sun, 7 Apr 2019 21:45:28 -0400 Subject: [PATCH] Add description of autocompleter callback Documented feature added by symfony/symfony#30997. --- components/console/helpers/questionhelper.rst | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/components/console/helpers/questionhelper.rst b/components/console/helpers/questionhelper.rst index 60651b83e5f..8edcbf9370c 100644 --- a/components/console/helpers/questionhelper.rst +++ b/components/console/helpers/questionhelper.rst @@ -179,6 +179,52 @@ will be autocompleted as the user types:: $bundleName = $helper->ask($input, $output, $question); } +In more complex use cases, it may be necessary to generate suggestions on the +fly, for instance if you wish to autocomplete a file path. In that case, you can +provide a callback function to dynamically generate suggestions:: + + use Symfony\Component\Console\Question\Question; + + // ... + public function execute(InputInterface $input, OutputInterface $output) + { + // This function is called whenever the input changes and new + // suggestions are needed. + $callback = function (string $input): array { + // Strip any characters from the last slash to the end of the + // string - this is considered a complete path and should populate + // our autocomplete suggestions. + $inputPath = preg_replace( + '%(/|^)[^/]*$%', + '$1', + $input + ); + + // All suggestions should start with the input the user has already + // provided (existing path), followed in this case by the contents + // of the referenced directory. Some suggestions may be ignored + // because they don't match the full string provided by the user, + // but the autocomplete helper will take care of that for us. + return array_map( + function ($suggestion) use ($inputPath) { + return $inputPath . $suggestion; + }, + @scandir($inputPath === '' ? '.' : $inputPath) ?: [] + ); + }; + + $question = new Question('Please provide the full path of a file to parse'); + $question->setAutocompleterCallback($callback); + + $filePath = $this->output->askQuestion($question); + } + +.. caution:: + + This example code allows unrestricted access to the host filesystem, and + should only ever be used in a local context, such as in a script being + manually invoked from the command line on a server you control. + Hiding the User's Response ~~~~~~~~~~~~~~~~~~~~~~~~~~