1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
13 // ParseFlags calls f.Parse(args) and prints appropriate error/help
14 // messages to stderr.
16 // The positional argument is "" if no positional arguments are
17 // accepted, otherwise a string to print with the usage message,
18 // "Usage: {prog} [options] {positional}".
20 // The first return value, ok, is true if the program should continue
21 // running normally, or false if it should exit now.
23 // If ok is false, the second return value is an appropriate exit
24 // code: 0 if "-help" was given, 2 if there was a usage error.
25 func ParseFlags(f FlagSet, prog string, args []string, positional string, stderr io.Writer) (ok bool, exitCode int) {
26 f.Init(prog, flag.ContinueOnError)
27 f.SetOutput(io.Discard)
31 if f.NArg() > 0 && positional == "" {
32 fmt.Fprintf(stderr, "unrecognized command line arguments: %v (try -help)\n", f.Args())
37 if f, ok := f.(*flag.FlagSet); ok && f.Usage != nil {
41 fmt.Fprintf(stderr, "Usage: %s [options] %s\n", prog, positional)
47 fmt.Fprintf(stderr, "error parsing command line arguments: %s (try -help)\n", err)