Should the usage message which is printed with e.g.
command -?
of a Unix command go to stderr or stdout, and why? Should it go to the same place if the user makes a mistake with an option?
Should the usage message which is printed with e.g.
command -?
of a Unix command go to stderr or stdout, and why? Should it go to the same place if the user makes a mistake with an option?
It should go to stdout, so you can type:
command --help | less
This is also recommended by the Gnu Coding Standards on --help
.
On the other hand, the usage message that you get when you use an invalid option or omit a required argument should go to stderr, because it's an error message, and you don't want it to feed into the next command in a pipeline.
When you use --help
, the usage message is the normal and expected output of the command. Therefore, it goes to stdout, so it can be piped to another command, like less
or grep
.
When you say command --bogus-option | other-command
, you don't want the usage message going to stdout, because it's now unexpected output that should not be processed by other-command
. Also, if the output of --help
is more than a handful of lines, then the usage error message should only contain a summary of the --help
output, and refer the user to --help
for additional details.
--help
is more than a few lines, then the usage message produced by an invalid option should be just a short summary saying to use --help
to see the complete information.
– cjm
Mar 08 '11 at 08:38
--help
was explicitly asked for, otherwise >0.
– ibonyun
Nov 27 '23 at 20:42
Because --help and -h are standard and because -? will can be interpreted by the shell.
– ctrl-alt-delor Mar 08 '11 at 09:53touch -- -l; ls -?
— you actually get a long listing as if you'd passed-l
to ls. – mattdm Mar 08 '11 at 12:56-h
option for other thing than a help message, then'-?'
(with the quotes) or-:
give you a good chance to get an error (and usage) message since:
and?
can't be valid options for anything usinggetopt(3)
. – Stéphane Chazelas Feb 08 '13 at 22:40