In an earlier tip, I had my command print validation errors and return static::FAILURE when the input was bad.
That worked, but I wondered if I could be more precise.
I was thinking of parallels to a controller. We have all sorts of HTTP response codes that provide additional information as to what kind of problem was encountered.
In an Artisan command, the FAILURE constant (exit code 1) is the catch-all for "something went wrong while running."
Was there a validation-specific exit code I could return instead?
We don't have anywhere near the variety of codes that an HTTP response does, but the underlying Symfony command offers one additional INVALID constant (exit code 2) that felt more appropriate to me.
Instead of a generic failure, I'm specifically saying that the command execution was invalid.
Some might argue that exit code 2 doesn't cleanly map to an input validation error, but I think it's perfectly acceptable and a slight improvement.
And I'm in good company.
Python's argparse exits with that same code 2 when a command is run with bad arguments, so treating it as the signal for invalid input is an existing convention.
Here to help,
Joel
P.S. Clear input handling is a security concern too. Get our security book.