Skip to content

[Console] Add return for execute() #17176

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
Aug 15, 2022
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
68 changes: 55 additions & 13 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ the following to your command::
{
// ...

public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Continue with this action?', false);
Expand Down Expand Up @@ -75,12 +75,16 @@ if you want to know a bundle name, you can add this to your command::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Commande::SUCCESS;
}

The user will be asked "Please enter the name of the bundle". They can type
Expand All @@ -99,7 +103,7 @@ from a predefined list::
use Symfony\Component\Console\Question\ChoiceQuestion;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -115,6 +119,8 @@ from a predefined list::
$output->writeln('You have just selected: '.$color);

// ... do something with the color

return Commande::SUCCESS;
}

.. versionadded:: 5.2
Expand Down Expand Up @@ -142,7 +148,7 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
use Symfony\Component\Console\Question\ChoiceQuestion;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -155,6 +161,8 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult

$colors = $helper->ask($input, $output, $question);
$output->writeln('You have just selected: ' . implode(', ', $colors));

return Commande::SUCCESS;
}

Now, when the user enters ``1,2``, the result will be:
Expand All @@ -172,7 +180,7 @@ will be autocompleted as the user types::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -182,6 +190,10 @@ will be autocompleted as the user types::
$question->setAutocompleterValues($bundles);

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Commande::SUCCESS;
}

In more complex use cases, it may be necessary to generate suggestions on the
Expand All @@ -191,7 +203,7 @@ provide a callback function to dynamically generate suggestions::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
$helper = $this->getHelper('question');

Expand All @@ -217,6 +229,10 @@ provide a callback function to dynamically generate suggestions::
$question->setAutocompleterCallback($callback);

$filePath = $helper->ask($input, $output, $question);

// ... do something with the filePath

return Commande::SUCCESS;
}

Do not Trim the Answer
Expand All @@ -228,7 +244,7 @@ You can also specify if you want to not trim the answer by setting it directly w
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -237,6 +253,10 @@ You can also specify if you want to not trim the answer by setting it directly w
$question->setTrimmable(false);
// if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
$name = $helper->ask($input, $output, $question);

// ... do something with the name

return Commande::SUCCESS;
}

Accept Multiline Answers
Expand All @@ -255,7 +275,7 @@ the response to a question should allow multiline answers by passing ``true`` to
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -264,6 +284,10 @@ the response to a question should allow multiline answers by passing ``true`` to
$question->setMultiline(true);

$answer = $helper->ask($input, $output, $question);

// ... do something with the answer

return Commande::SUCCESS;
}

Multiline questions stop reading user input after receiving an end-of-transmission
Expand All @@ -278,7 +302,7 @@ convenient for passwords::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -288,6 +312,10 @@ convenient for passwords::
$question->setHiddenFallback(false);

$password = $helper->ask($input, $output, $question);

// ... do something with the password

return Commande::SUCCESS;
}

.. caution::
Expand All @@ -311,13 +339,15 @@ convenient for passwords::
use Symfony\Component\Console\Question\ChoiceQuestion;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
QuestionHelper::disableStty();

// ...

return Commande::SUCCESS;
}

Normalizing the Answer
Expand All @@ -333,7 +363,7 @@ method::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -345,6 +375,10 @@ method::
});

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Commande::SUCCESS;
}

.. caution::
Expand All @@ -367,7 +401,7 @@ method::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -385,6 +419,10 @@ method::
$question->setMaxAttempts(2);

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Commande::SUCCESS;
}

The ``$validator`` is a callback which handles the validation. It should
Expand Down Expand Up @@ -423,7 +461,7 @@ You can also use a validator with a hidden question::
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = $this->getHelper('question');
Expand All @@ -443,6 +481,10 @@ You can also use a validator with a hidden question::
$question->setMaxAttempts(20);

$password = $helper->ask($input, $output, $question);

// ... do something with the password

return Commande::SUCCESS;
}

Testing a Command that Expects Input
Expand Down