Check if Flag Was Provided in Go

28,182

Solution 1

Use the flag.Visit() function:

func isFlagPassed(name string) bool {
    found := false
    flag.Visit(func(f *flag.Flag) {
        if f.Name == name {
            found = true
        }
    })
    return found
}

Solution 2

The built-in flag types don't support distinguishing default values and explicit assignment to the default value. However, the flag package is quite flexible, and allows you to roll your own type that does, using the flag.Value interface.

Here's a full example that contains a string flag which records if it's been assigned to.

package main

import (
    "flag"
    "fmt"
)

type stringFlag struct {
    set   bool
    value string
}

func (sf *stringFlag) Set(x string) error {
    sf.value = x
    sf.set = true
    return nil
}

func (sf *stringFlag) String() string {
    return sf.value
}

var filename stringFlag

func init() {
    flag.Var(&filename, "filename", "the filename")
}

func main() {
    flag.Parse()
    if !filename.set {
        fmt.Println("--filename not set")
    } else {
        fmt.Printf("--filename set to %q\n", filename.value)
    }
}

Here's some example runs:

$ go run a.go -filename=abc
--filename set to "abc"

$ go run a.go -filename=
--filename set to ""

$ go run a.go
--filename not set

Solution 3

The issue with using a custom flag type (the stringFlag example in this thread) is you'll slightly upset the PrintDefaults output (i.e. --help). For example, with a string username flag and a stringFlag servername flag, --help looks like this:

-server value
    server:port (default localhost:1234)
-username string
    username (default "kimmi")

Note these are both string arguments as far as the user is concerned, but presented differently as a stringFlag is not a string.

flag's Flagset has an internal map that includes the flags that were declared ('formal') and those actually set ('actual'). The former is available via Lookup(), though alas the latter is not exposed, or you could just write:

var servername = flag.String("server", "localhost:8129", "server:port")

flag.Parse()

if f := flag.CommandLine.LookupActual("server"); f != nil {
    fmt.Printf("server set to %#v\n", f)
} else {
    fmt.Printf("server not set\n")
}

Seems like the best you can do, if you want consistent PrintDefaults() output, is to use Visit to extract your own view of 'actual' (VisitAll does the same thing with 'formal'):

var servername = flag.String("server", "localhost:8129", "server:port")

flag.Parse()

flagset := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { flagset[f.Name]=true } )

if flagset["server"] {
    fmt.Printf("server set via flags\n")
} else {
    fmt.Printf("server not explicitly set, using default\n")
}

Solution 4

To use a dynamic default value for a flag, create the flag with the default set to the dynamic value:

func main() {
  flagHost = flag.String(flagHostFlagKey, computedHostFlag(), "...")
  flag.Parse()
  // *flagHost equals the return value from computedHostFlag() if 
  // the flag is not specified on the command line.
  ...
}

With this approach, it's not necessary to detect if the flag is specified on the command line. Also, help shows the correct default.

If the computed value depends on other flags or is expensive to calculate, then use the approach suggested in one of the other answers.

Solution 5

Face with same problem, but have even complex case with bool flag, in this case computedHostFlag() not working, since you can provide to flag creation only true or false. "type stringFlag struct" solution also not the best, since ruin idea of default values.

Solve it in this way: create two sets of flags, with different default values, after parse - just check - if flag in first flagset have the same value that flag from second flagset - that it means that flag value was provided by user from command line. If they different - than this mean that flag was set by default.

package main

import (
    "fmt"
    "flag"
)

func main() {
    args := []string{"-foo="}

    flagSet1 := flag.NewFlagSet("flagSet1", flag.ContinueOnError)
    foo1 := flagSet1.String("foo", "-", ``)
    boolFoo1 := flagSet1.Bool("boolfoo", false, ``)
    flagSet1.Parse(args)

    flagSet2 := flag.NewFlagSet("flagSet2", flag.ContinueOnError)
    foo2 := flagSet2.String("foo", "+", ``)
    boolFoo2 := flagSet2.Bool("boolfoo", true, ``)
    flagSet2.Parse(args)

    if *foo1 != *foo2 {
        fmt.Println("foo flag set by default")
    } else {
        fmt.Println("foo flag provided by user")
    }

    if *boolFoo1 != *boolFoo2 {
        fmt.Println("boolfoo flag set by default")
    } else {
        fmt.Println("boolfoo flag provided by user")
    }
}

playground: https://play.golang.org/p/BVceE_pN5PO , for real CLI execution, you can do something like that: https://play.golang.org/p/WNvDaaPj585

Share:
28,182
Kyle Brandt
Author by

Kyle Brandt

Developer at Grafana Labs - formerly SRE at Stack Overflow.

Updated on May 12, 2020

Comments

  • Kyle Brandt
    Kyle Brandt about 4 years

    With the flag package, is there a good way to distinguish if a string flag was passed?

    For example, when the flag is not passed, I want to set it to a dynamic default value. However, I want to set it to empty if the flag was provided but with a value of "".

    Current I am doing the following:

    flagHost = flag.String(flagHostFlagKey, "", "...")
    ...
    setHostname := false
    for _, arg := range os.Args {
        if arg == "-"+flagHostFlagKey {
            setHostname = true
        }
    }
    
    if !setHostname {
         ...
    

    Which seems to work fine, but is kind of ugly. Is there a better way while staying with the standard flag package?

  • Brian
    Brian over 8 years
    Plus, if computedHostFlag() is idempotent, this has the added benefit of making the usage (./foo -h) display an accurate default value.
  • Cerise Limón
    Cerise Limón over 8 years
    The answer notes that the help shows the default. The function need not be idempotent as it's only called once.
  • Brian
    Brian over 8 years
    Whoops, completely missed that it was already included. Sorry! If it's not idempotent though, what you see with the Usage may differ than what the value is when it is subsequently run.
  • Cerise Limón
    Cerise Limón over 8 years
    Oh, you are right. It need to return same value each time program is executed.
  • ardnew
    ardnew over 5 years
    a word of warning before you go refactoring your code to implement this: if you wrap any bool flags in a flag.Value implementation, you won't be able to set them at invocation without argument. in other words, where you used to be able to set bool flags by simply using prog -boolflag progargs, you'll have to instead use prog -boolflag true progargs. otherwise, progargs gets swallowed up as the argument to -boolflag and won't be available later in Args().
  • Vojtech Vitek
    Vojtech Vitek over 4 years
    flag.Lookup() returns the Flag structure of the named flag. It doesn't tell you if the flag was actually provided in the CLI or not.
  • ardnew
    ardnew over 4 years
    I was so ready to counter the above comment and criticize their carelessness, and then I realized it was my comment from over a year ago... anyway, it is not correct (anymore?). From the go docs, in your flag.Value implementation: If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument.
  • emersion
    emersion about 4 years
    This is accessing unexported fields. Go doesn't guarantee stability for private APIs. Programs shouldn't do this, this can break at any Go release.
  • ec-m
    ec-m almost 4 years
    Note that this needs to be called after flag.Parse.