Skip to content

[Console] Define constants for main exit codes. #35431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Chi-teck opened this issue Jan 22, 2020 · 9 comments · Fixed by #35478
Closed

[Console] Define constants for main exit codes. #35431

Chi-teck opened this issue Jan 22, 2020 · 9 comments · Fixed by #35478
Labels
Console DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature

Comments

@Chi-teck
Copy link
Contributor

Description
Console commands must return exit code. I propose we define constants for most used exit statuses like it is done in C programming language.
https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
That would make code a bit more clear.

Example
Before:
return 0;
After:
return self::EXIT_SUCCESS;

@ro0NL
Copy link
Contributor

ro0NL commented Jan 22, 2020

you can define such consts in your application for your commands, i dont think it's much of value for core commands, nor is implying everyone should use this. Personally im fine with ints :)

IMHO it's the same topic as http status codes => #22023

@javiereguiluz
Copy link
Member

javiereguiluz commented Jan 22, 2020

@ro0NL this is indeed a similar situation to the constants for the HTTP Status Codes. We rejected to use those in Symfony's own code ... but we defined those constants in case developers want to use them in their apps (I do for example 😊).

My opinion: I'd love to have these constants for command exit codes. Not for all the codes defined here, but for the most common:

public static $exitCodes = [
0 => 'OK',
1 => 'General error',
2 => 'Misuse of shell builtins',
126 => 'Invoked command cannot execute',
127 => 'Command not found',
128 => 'Invalid exit argument',

Why? Because I always have problems remembering if "success exit" is 0 or 1 ... and when I remember that "success == 0" ... it's confusing because this is PHP, so 0 = false = error and 1 = true = success 😕 Now that Symfony forces you to define the exit code, these constants would make things more DX-friendly.

@javiereguiluz javiereguiluz added Console Feature DX DX = Developer eXperience (anything that improves the experience of using Symfony) labels Jan 22, 2020
@Chi-teck
Copy link
Contributor Author

@javiereguiluz do you know any real use cases where a command returns status greater than 1?
I think having just two constants like glibc would probably be sufficient.

@ro0NL
Copy link
Contributor

ro0NL commented Jan 22, 2020

i was a bit skeptical about consts for just int(0) or int(1), having more defined exit codes is more valueable 👍

personally i dont think of return 0/1; as a potential implicit boolean value.

im also curious if it belongs to Process or Console component :D

@javiereguiluz
Copy link
Member

javiereguiluz commented Jan 22, 2020

@Chi-teck although it's hard to find a standard list of exit codes ... most Linux-flavored OS use these codes: https://github.com/freebsd/freebsd/blob/master/include/sysexits.h

Some of them are useful to us: EX_DATAERR (65) when the user input is wrong for any reason, EX_IOERR (74) when some I/O operation failed on some file, EX_NOPERM (77) when the user does not have permission to run the command, etc.

@alister
Copy link

alister commented Jan 26, 2020

I've returned specific exit codes from a (php) script before so specific actions can happen in the calling (shell) script, @Chi-teck.

A blogpost, one of a series I wrote about using queues, described how you could potentially slow things down, or exit the whole thing, using the return values.

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Jan 26, 2020

@Chi-teck
Copy link
Contributor Author

I would start with only two constants for the following reasons.

  1. The vast majority of console applications probably use only two status codes (0/1).
  2. Exit codes are not standardized across different platforms.
  3. If we have a good reason it's never too late to introduce constants for other exit codes.

@fabpot
Copy link
Member

fabpot commented Jan 26, 2020

I agree that we should only have constants for 0 and 1.

@fabpot fabpot closed this as completed Jan 29, 2020
fabpot added a commit that referenced this issue Jan 29, 2020
This PR was squashed before being merged into the 5.1-dev branch (closes #35478).

Discussion
----------

[Console] Add constants for main exit codes

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? |no
| Tickets       | Fix #35431
| License       | MIT

Commits
-------

5f6a1ac [Console] Add constants for main exit codes
weaverryan added a commit to symfony/maker-bundle that referenced this issue Aug 25, 2020
…possible (me)

This PR was merged into the 1.0-dev branch.

Discussion
----------

Use Command:SUCCESS constant for command exit code when possible

This is follow-up for symfony/symfony#35431

It would also align generated code with an example in the documentation.
https://symfony.com/doc/current/console.html#creating-a-command

Commits
-------

d40ec33 Use Command:SUCCESS constant for command exit code when possible
saylor-mik87786 added a commit to saylor-mik87786/maker-bundle that referenced this issue Jun 3, 2025
…possible (me)

This PR was merged into the 1.0-dev branch.

Discussion
----------

Use Command:SUCCESS constant for command exit code when possible

This is follow-up for symfony/symfony#35431

It would also align generated code with an example in the documentation.
https://symfony.com/doc/current/console.html#creating-a-command

Commits
-------

d40ec33 Use Command:SUCCESS constant for command exit code when possible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Console DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants