Conditional output with a Symfony command, depending on the current verbosity level

Published on 2021-09-18 • Modified on 2021-09-18

This snippet shows how to conditionally display stuff with a Symfony command, depending on the current verbosity level. In this case, we can use the getVerbosity() function of the OutputInterface object and test that the current level is above or equal to the verbose level constant. Note that the writeXX() functions of the OutputInterface accept a verbosity level as the second argument, so you don't even have to use an if statement.


    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
            $io->note('This note will only be displayed when using at least the verbose mode option for the command "-v"');
        }
        $output->writeln('This line will only be displayed when using at least the verbose mode option for the command "-v"', OutputInterface::VERBOSITY_VERBOSE);

 More on Stackoverflow   Read the doc  More on the web  Random snippet