Skip to content
Open
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
57 changes: 57 additions & 0 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,63 @@ the response to a question should allow multiline answers by passing ``true`` to
Multiline questions stop reading user input after receiving an end-of-transmission
control character (``Ctrl-D`` on Unix systems or ``Ctrl-Z`` on Windows).

Setting a Timeout for User Input
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 7.4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let move this at the end of this section please


The timeout functionality for questions was introduced in Symfony 7.4.

You can set a maximum time limit for user input by using the
:method:`Symfony\\Component\\Console\\Question\\Question::setTimeout` method.
If the user doesn't respond within the specified timeout, a
:class:`Symfony\\Component\\Console\\Exception\\MissingInputException` will be thrown::

use Symfony\Component\Console\Exception\MissingInputException;
use Symfony\Component\Console\Question\Question;

// ...
public function __invoke(InputInterface $input, OutputInterface $output): int
{
// ...
$helper = new QuestionHelper();

$question = new Question('Please enter your answer');
$question->setTimeout(30); // 30 seconds timeout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$question->setTimeout(30); // 30 seconds timeout
$question->setTimeout(seconds: 30);

would be much better, but lets wait for @xabbuh and @javiereguiluz, because IIRC we don't use named arguments in the docs, as they are not covered by BC


try {
$answer = $helper->ask($input, $output, $question);
// ... do something with the answer
} catch (MissingInputException $exception) {
$output->writeln('No input received within timeout period.');
// ... handle timeout
}

return Command::SUCCESS;
}

This is particularly useful when you have interactive questions inside database
transactions or other time-sensitive operations where hanging indefinitely
could cause problems.

.. note::

The timeout only applies to interactive input streams. For non-interactive
streams (like pipes or files), the timeout setting is ignored and the
question behaves normally.

You can also use timeouts with other question types such as
:class:`Symfony\\Component\\Console\\Question\\ConfirmationQuestion` and
:class:`Symfony\\Component\\Console\\Question\\ChoiceQuestion`::

use Symfony\Component\Console\Question\ConfirmationQuestion;

// ...
$question = new ConfirmationQuestion('Do you want to continue?', false);
$question->setTimeout(10); // 10 seconds timeout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$question->setTimeout(10); // 10 seconds timeout
$question->setTimeout(seconds: 10);

See comment above


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

Hiding the User's Response
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down