Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 scandir() which opens the door for a dot dot slash attack. Sure, it‘s your own system but who knows who‘s going to read the docs and it what context the reader is using the example :) wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down