views:

165

answers:

4

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?

+3  A: 

This can only be opinion, but I think writing to stderr is the best thing to do. That way the usage message appears if the user makes a mistake even if the normal output has been re-directed.

anon
+1  A: 

According to me, the criteria is how emergence is the information. If it needs immediate reaction or attention, I put it into stderr (cause it's unbuffered). If it is somehow informative and do not regard any errors it is for stdout.

anthares
+1  A: 

I'd use STDERR since simply putting it to STDOUT might cause problems with piped output and it will appear in the logs for cronjobs so you notice the mistake easier.

dbemerlin
+8  A: 

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.

OregonGhost
I agree. You should also consider exit code - if the program is called with wrong arguments, it should return 1, if it is called with --help or similar, it should return with 0.
Alex Brown
Thanks for the precision about stdout when explicitly requested (such as --help). That made the accepted answer.
Frór
Thank you Alex for the precision about 0 and 1. That would have been another question without your answer here ;)
Frór
@Alex: Totally agree, though unfortunately there's no universally accepted standard on return values. Zero is a good success value though in my opinion.
OregonGhost
@OregonGhost: There is an accepted standard. Programs always return zero on success and !0 on error (return values for errors are not defined though, they just have to be != 0).
dbemerlin
@dbemerlin: That is theoretical. I have a lot of command line tools here that return some value other than zero for success. One of them returns 42, but there are others that return a lot of things. This means you simply can't count on a command line program returning zero for success, which (in my opinion) means there is no universally accepted standard. It would be great if it was that easy, but it isn't.
OregonGhost