Merge branch 'main' into 20831-user-table-locks
[arvados.git] / lib / cmd / parseflags.go
index 3e872fcd11986a2ba69786a7da3d3267128f2704..707cacbf524613e007ab5a6b9bd40abb1eaf5248 100644 (file)
@@ -8,8 +8,13 @@ import (
        "flag"
        "fmt"
        "io"
+       "reflect"
 )
 
+// Hack to enable checking whether a given FlagSet's Usage method is
+// the (private) default one.
+var defaultFlagSet = flag.NewFlagSet("none", flag.ContinueOnError)
+
 // ParseFlags calls f.Parse(args) and prints appropriate error/help
 // messages to stderr.
 //
@@ -34,7 +39,12 @@ func ParseFlags(f FlagSet, prog string, args []string, positional string, stderr
                }
                return true, 0
        case flag.ErrHelp:
-               if f, ok := f.(*flag.FlagSet); ok && f.Usage != nil {
+               // Use our own default usage func, not the one
+               // provided by the flag pkg, if the caller hasn't set
+               // one. (We use reflect to determine whether f.Usage
+               // is the private defaultUsage func that
+               // flag.NewFlagSet uses.)
+               if f, ok := f.(*flag.FlagSet); ok && f.Usage != nil && reflect.ValueOf(f.Usage).String() != reflect.ValueOf(defaultFlagSet.Usage).String() {
                        f.SetOutput(stderr)
                        f.Usage()
                } else {