On Thu, Jul 20, 2017 at 07:23:23AM +1000, Rob Pike wrote: >>> I realize than running `mybinary --help` returns a nice help message >>> along with a exit status 2. >>> What is the reason it doesn't return 0? [...] >> It's reasonable to consider calling a command with a flag that is not >> defined an error (whether that flag is -help or -verbose or -whatever). It >> is common to return non 0 on error. > The Unix tradition, fading away like all the other Unix traditions, is that > exit code 0 is for success, 1 is for failed execution, and 2 is for > reporting that the usage was incorrect, preventing it from running. > Although one could make the case that "-help" did what it was supposed to > do, tradition says the command didn't run because of the arguments, so the > exit code should be 2. > > "grep --help" doesn't grep anything because of what the arguments said. I > just tried this with BSD grep and got exit code 2. For some reason, grep > still obeys the Unix tradition. Someone will fix that soon, no doubt, but > for now let's be consistent.
For what it worth, GNU coding guidelines suggest that running a tool explicitly passing it the "--help" (or "-h" or another option with the same meaning) is a conscious decision of the user and hence printing the help message is carrying out the intended task. Hence the tools are supposed to behave differently when run with "--help" and when run with an invalid combination of the options: in the former case they are supposed to print a reasonably complete help synopsis and exit with status 0, otherwise they should print a terse message explaining what went wrong and how to get help and exit with a non-zero exit code. To demonstrate how GNU grep behaves: ~$ grep Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. ~$ echo $? 2 ~$ grep --help >/dev/null ~$ echo $? 0 I personally find such behaviour to be reasonably logical. (Not that I'm to blame the Go's flag package though.) -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.