Reputation: 5283
sandbox/tmp/main.go
package main
import (
"flag"
"sandbox/tmp/pac"
)
func main() {
flag.Set("str_flag", "new_val")
// flag.Set("bool_flag", true) // How to set this?
flag.Parse()
pac.PrintFlags()
}
sandbox/tmp/pac/pac.go
package pac
import "flag"
var (
boolFlag = flag.Bool("bool_flag", false, "")
strFlag = flag.String("str_flag", "def_val", "")
)
func PrintFlags() {
println(*boolFlag)
println(*strFlag)
}
What's a complement for flag.Set()
for boolean flags?
Upvotes: 0
Views: 375
Reputation: 418455
Simply use the string representation that you would provide in the command line, e.g "true"
:
flag.Set("bool_flag", "true")
Quoting from Package doc: Command line flag syntax:
Boolean flags may be:
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
So the following also work:
flag.Set("bool_flag", "1")
flag.Set("bool_flag", "t")
flag.Set("bool_flag", "T")
flag.Set("bool_flag", "TRUE")
flag.Set("bool_flag", "True")
Upvotes: 3