From 0509bac12eb795214bb50e746c59aea2f9b3cc31 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 27 Aug 2021 11:20:16 -0700 Subject: [PATCH] Fail immediately if there's a getopt parse error (#30) The getopt code doesn't appear to fully respect ErrorHandling so unrecognized flags result in Usage being displayed but not an Exit(). https://github.com/rsc/getopt/blob/20be20937449f18bb9967c10d732849fb4401e63/getopt.go#L274-L277 In our application, this was causing Usage to be displayed twice. To fix this, we catch the lower-level Parse() error and bail immediately. --- cmd/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index d2387f8..4d15976 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -127,7 +127,9 @@ func lint(l *thriftcheck.Linter, filenames []string) (thriftcheck.Messages, erro func main() { // Parse command line flags - getopt.Parse() + if err := getopt.CommandLine.Parse(os.Args[1:]); err != nil { + os.Exit(1 << uint(thriftcheck.Error)) + } if *helpFlag { flag.Usage() os.Exit(0)