3

It is possible in PHP to create console command waiting for an input from user like this or like this. Is there any 'magento-way' or above approaches can be used?

3

Magento 2 using symfony for CLI commands. So symfony is having a all these kind of featurs.

To create CLI comment in magento check here

Then in your module console command file eg:

Inchoo\Console\Console\Command\HelloWorldCommand.php

<?php

namespace Inchoo\Console\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\ObjectManagerInterface;
class HelloWorldCommand extends Command
{
    protected $_objectManager;

    public function __construct(ObjectManagerInterface $objectManager){
        $this->_objectManager = $objectManager;
    }
    protected function configure()
    {
        $this->setName('inchoo:hello_world')->setDescription('Prints hello world.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $question = $this->_objectManager->create('\Symfony\Component\Console\Question\ConfirmationQuestion', ['question'=>'your question', 'default' => FALSE]);
        echo $ans = $helper->ask($input, $output, $question);
        if ($ans == 'yes') {
           $output->writeln('Hello World!');
        }else{
            $output->writeln('Noooooooo');
        }
        $output->writeln('Done!');
    }

}

Refer this for other CLI command like Multiple Choices question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.