Should the command line "usage" be printed on stdout or stderr?
When printing the "usage" of an application, should it be done on stdout or on stderr?
Depending on the application I've seen several cases, but there doesn't seem to be one rule. Maybe I'm mistaken and there is one good practice. In that case, what is it?
Never thought about it, but why not write the usage instructions to stderr if the program was called with no or wrong arguments, and write it to stdout when called with a --help
(or similar) argument? This way, if the usage is shown because of an error, it goes to stderr, and if it's not an error because the user requested it, it goes to stdout. Seems logical, somehow.
I agree that explicitly requested "usage" (through a -h, -? or --help option) should go to stdout while "usage" that is printed in response to incorrect syntax or other errors should go to stderr.
However, note that the increasingly popular popt library (which handles command line parsing; its name stands for "parse options") includes a facility for automatically generated help and that it always sends that to stderr. I quote the popt man page:
When --usage or --help are passed to programs which use popt's automatic help, popt displays the appropriate message on stderr as soon as it finds the option, and exits the program with a return code of 0.
I believe this to be a popt bug, but the problem is that POSIX (or ISO C, to which it defers) never defined what was meant by "diagnostic output". Just read 'man stderr' or POSIX.1-2008.