I'm using the flag module to parse my flags, but want to have at least one positional argument. How do I show the usage help when not enough positional arguments are present, as I would in python with parser.error
?
Currently, I am manually calling os.Exit
, but that feels really cumbersome for what should be a simple error:
package main
import "flag"
import "fmt"
import "os"
func main() {
flag.Parse()
if flag.NArg() != 1 {
println("This program needs exactly one argument")
flag.Usage()
os.Exit(2)
}
fmt.Printf("You entered %d characters", len(flag.Args()[0]))
}