use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyCommand extends Command { protected function execute(InputInterface $input, OutputInterface $output) { if ($input->isInteractive()) { // Interactive behavior $output->writeln('This is an interactive command.'); } else { // Non-interactive behavior $output->writeln('This is a non-interactive command.'); } } }
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; class MyCommand extends Command { protected function execute(InputInterface $input, OutputInterface $output) { $questionHelper = $this->getHelper('question'); if ($input->isInteractive()) { // Prompt for confirmation $question = new ConfirmationQuestion('Are you sure you want to do this? '); $confirmed = $questionHelper->ask($input, $output, $question); if ($confirmed) { // Perform some action $output->writeln('Action performed.'); } else { // Cancelled $output->writeln('Cancelled.'); } } else { // Non-interactive, perform action without confirmation $output->writeln('Action performed.'); } } }In this example, the `execute()` method of a command is checking whether the input is interactive using `$input->isInteractive()`. If it is interactive, it prompts the user with a confirmation question using the `ConfirmationQuestion` class and waits for a response using the `ask()` method of the `QuestionHelper`. If the user confirms, it performs some action and outputs "Action performed." If the user cancels, it outputs "Cancelled." If it is not interactive, it performs the action without confirmation. Package library: `symfony/console`