Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
Add description of autocompleter callback#11349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I‘m not sure this is the best example. What you‘re doing is passing on user input to ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Well, I added the feature because I wanted to autocomplete subsequent words in a command chain, but the most obvious use case seems to be path autocompletion (particularly since that's behaviour we're accustomed to in bash as well). I'm open to alternatives, but that's the best I could come up with in terms of something that was a) useful, and b) straightforward enough to be readable as an example. Maybe a disclaimer would be an appropriate compromise? | ||
| 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 | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||