Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
javiereguiluz merged 1 commit intosymfony:masterfromMikkelPaulson:console-autocompleter-callback
Apr 9, 2019
Merged
Changes fromall 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 deletionscomponents/console/helpers/questionhelper.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 toscandir() 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
ContributorAuthor

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

[8]ページ先頭

©2009-2025 Movatter.jp