How to set the exit status code in a Symfony2 command?

command-line, symfony

Solution

In the base `Command` class:

    if ($this->code) {
        $statusCode = call_user_func($this->code, $input, $output);
    } else {
        $statusCode = $this->execute($input, $output);
    }

    return is_numeric($statusCode) ? (int) $statusCode : 0;

So simply return the exit code from your `execute()` function. Your console command will exit with this code as long as it is a numeric value.

Problem

What's the proper way for setting the exit status code in a Symfony2 command? In plain php you can do this with `exit(123)`. But I guess Symfony2 has an OOP way. Is that right? I couldn't find anything on the docs though. I need this mostly because I want to be able to do something like this in Linux: `app/console my:command || { echo "Something went wrong, I'm gonna call handle_disaster now"; handle_disaster; }`

Original source