I have an app that will fprintf
both help and error messages to stderr
.
Should I send messages to stdout
if I am exiting with status EXIT_SUCCESS
(such as when I issue the --help
option to my app)?
Likewise, should I keep sending error messages to stderr
on EXIT_FAILURE
?
Or should I send all help and error messages to stdout
?
What are general conventions for this with POSIX-compliant UNIX applications?
Clearly error messages should go to stderr
, because you don't want to capture them when redirecting standard output.
When the usage is displayed because some command line option was invalidly used, then it's being shown as (part of) an error message. So it should go to stderr
and cause EXIT_FAILURE
.
When the usage is being displayed because the user asked for it via --help
, then it's being shown as the desired behaviour of invoking the command. So it should go to stdout
and the command should succeed with EXIT_SUCCESS
.
This is briefly covered in the GNU coding standards.