New in Symfony 4.4: Console Improvements

The Console component is one of the most popular and mature Symfony components. Even if some developers consider it already feature-complete, in each new Symfony version we add some small new features to it.

Make it mandatory to return the command exit status

Contributed by
Jan Schädlich
in #33775.

The exit status is a number passed to the parent process (or caller) when a process or command has finished executing its task. A 0 exit status means that the command run successfully and any other number means some error.

However, in Symfony commands it's common to not return this exit status. In fact, it's common to not return anything from the execute() command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// src/Command/CreateUserCommand.php
namespace App\Command;

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

class CreateUserCommand extends Command
{
    protected static $defaultName = 'app:create-user';

    // ...

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
    }
}

In Symfony 4.4 we've deprecated this behavior and you are encouraged to return an integer with the exit status of your command:

1
2
3
4
5
6
protected function execute(InputInterface $input, OutputInterface $output)
{
    // ...

    return 0;
}

Returning the exit status is mandatory in Symfony 5, so better start adding those returns in your commands so you are ready to upgrade.

Configure the trimming of answers

Contributed by
Hamza Amrouche
in #31626.

When using the Question Helper to ask questions in the console, the answer input by the user is trimmed automatically. Although this is the most common behavior by far, in some edge-cases this is not desirable.

In Symfony 4.4, we've added a new setTrimmable() method so you can change the default behavior:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Symfony\Component\Console\Question\Question;

public function execute(InputInterface $input, OutputInterface $output)
{
    $helper = $this->getHelper('question');

    $question = new Question('What is the secret code?');
    $question->setTrimmable(false);
    // ...
}

If the user inputs ' abc 1234 ', that's exactly the value you'll get, instead of the default abc 1234 value.

A standard way of disabling ANSI color escape codes

Contributed by
Jordi Boggiano in #34252.

Symfony commands use lots of ANSI escape codes to add color to their output. Although most people like that, some people prefer to disable this feature completely. Since day one, Symfony has allowed to disable this by adding the --no-ansi option when executing a command.

However, the problem is that each project, tool and library defines a different way of disabling ANSI codes. That's why the no-color.org project has proposed to use the NO_COLOR env var as the standard way of disabling ANSI codes in every piece of software.

In Symfony 4.4 we added support for this env var (but we kept the --no-ansi option too) so you can now disable ANSI codes by running your commands like this:

1
$ NO_COLOR=1 php bin/console app:my-command

Comments

Neat!
Login with SymfonyConnect to post a comment