Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 282
Olivier Grégoire Avatar asked Feb 04 '10 12:02

Olivier Grégoire


People also ask

Should I log to stderr or stdout?

Regular output (the actual result of running the program) should go on stdout , things like you mentioned (e.g. diagnostic, notice, warning, error) on stderr . If there is no "regular output", I would say that it doesn't really matter which one you choose.

Should I print to stderr?

It is good practice to redirect all error messages to stderr , while directing regular output to stdout . It is beneficial to do this because anything written to stderr is not buffered, i.e., it is immediately written to the screen so that the user can be warned immediately.

Does stderr print to terminal?

The error message that is delivered via stderr is still sent to the terminal window. We can check the contents of the file to see whether the stdout output went to the file. The output from stdin was redirected to the file as expected. The > redirection symbol works with stdout by default.

What is the difference between stdout and stderr?

stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error. It is invoked whenever a command faces an error, then that error message gets stored in this data stream.


2 Answers

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.

like image 131
OregonGhost Avatar answered Sep 21 '22 23:09

OregonGhost


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.

like image 37
Urhixidur Avatar answered Sep 21 '22 23:09

Urhixidur