Threads for WillAbides

  1. 7

    The flag module in the standard library, which is commonly used for argument parsing, outputs help text to stderr instead of stdout, even when the user has explicitly requested the help text. This means that for every Go binary that you are using, you almost certainly need an extra 2>&1 redirection when piping the output of –help to a pager.

    It’s a small thing, but this one really gets under my skin. That said, you can deal with it if you’re stubborn and willing to do a bit of extra work.

    • flags.SetOutput(io.Discard) keeps Go from sending output to stderr.
    • Check manually whether the user requested help.
    • If they did, print usage to stdout and exit.

    It shouldn’t be even this hard, but it’s easy enough to fix.

    1. 3

      In my experience, most go programs with enough help text that it matters where help output is written don’t use the stdlib flags package anyway. They typically use github.com/spf13/cobra (although I prefer github.com/alecthomas/kong).