The Symfony Console Component is designed to be both flexible and intuitive. It allows developers to define commands with a clear structure, making it easy to manage complex CLI applications. Each command can be associated with a specific class, which contains the logic for executing the command. Additionally, the component supports features such as auto-generated help messages, input validation, and interactive prompts.

To begin using the Symfony Console Component, you need to install it via Composer. If you're working on a Symfony project, the component is already included. For standalone applications, you can install it using the following command:

composer require symfony/console

Once installed, you can start creating your first console command.


Creating a Basic Console Command

To create a new command, you need to define a class that extends Symfony\Component\Console\Command\Command. This class must implement the execute method, which is called when the command is run.

Here is an example of a basic command that outputs a message:

<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GreetingCommand extends Command
{
    protected static $defaultName = 'greet';

    protected function configure()
    {
        $this->setDescription('Display a greeting message.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('<info>Hello, world!</info>');
        return Command::SUCCESS;
    }
}

To run this command, you need to create an instance of Symfony\Component\Console\Application and register the command:

<?php

require 'vendor/autoload.php';

use Symfony\Component\Console\Application;

$application = new Application();
$application->add(new GreetingCommand());
$application->run();

When executed, this script will display the message Hello, world! in green text.


Defining Command Arguments and Options

The Symfony Console Component allows you to define arguments and options for your commands, making them more versatile and user-friendly. Arguments are positional parameters, while options are named flags that can be used to modify the behavior of a command.

Here’s an example of a command that accepts a name as an argument and a greeting as an option:

<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GreetingCommand extends Command
{
    protected static $defaultName = 'greet';

    protected function configure()
    {
        $this->setDescription('Display a greeting message.')
             ->addArgument('name', InputArgument::REQUIRED, 'The name to greet.')
             ->addOption('greeting', null, InputOption::VALUE_REQUIRED, 'The greeting to use.', 'Hello');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        $greeting = $input->getOption('greeting');

        $output->writeln("<info>{$greeting}, {$name}!</info>");
        return Command::SUCCESS;
    }
}

You can run this command using the following syntax:

php script.php greet John --greeting=Hi

This will output: Hi, John!


Handling Input and Output

The Symfony Console Component provides several methods for handling input and output, such as reading user input, displaying messages, and formatting text with colors and styles.

Here’s an example of a command that prompts the user for input and displays the result:

<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class InputCommand extends Command
{
    protected static $defaultName = 'input';

    protected function configure()
    {
        $this->setDescription('Prompt the user for input and display it.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $helper = $this->getHelper('question');
        $question = new Question('What is your name? ', 'Guest');
        $name = $helper->ask($input, $output, $question);

        $output->writeln("<comment>Hello, {$name}!</comment>");
        return Command::SUCCESS;
    }
}

This command will prompt the user with the question What is your name? and display a greeting message with the entered name.


Comparison of Console Components

FeatureSymfony Console ComponentOther CLI Libraries (e.g., CLIFramework)
FlexibilityHighMedium
Integration with SymfonyFullLimited
Input/Output HandlingRichBasic
Help Message GenerationAuto-generatedManual
Argument/Option SupportFullPartial

Learn more with official resources