How to handle the `flag.ErrHelp` error?

I’m using the flag package to build my CLI application, but I’m facing a discrepancy. I want to utilize the flag.ErrHelp error which is thrown when the -h or -help flags are invoked from the command line, as the docs say. But weirdly, I’m not able to observe this behaviour. Here’s my minimal test example:

// test.go
package main

import (

func main() {
	err := flag.CommandLine.Parse(os.Args[1:])

Now when I invoke the h flag, I get the following:

$ go run test.go -h
Usage of /tmp/go-build225264989/b001/exe/test:
exit status 2

It indifferently exits with exit status 2, the same as if I’ve invoked some undefined flag, but the docs say that it should invoke flag.ErrHelp. Is it a bug with the package or indeed some mistake with my implementation? According to the docs, flag.ErrHelp should be thrown even if the help flag isn’t defined at all!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.