FuzzyAmi
FuzzyAmi

Reputation: 8119

How to pass boolean arguments to go flags

I have a simple boolean flag I wish to pass args to:

import (
    "flag"
    ...
 )

var debugMode = flag.Bool("debug", false, "run in debug mode")
flag.Parse()
if *debugMode == true {
    //print something
}

This code compiles and runs - but the variable is always true. I use the following call:

my_application -debug false

and it's never false. What am I doing wrong?

Upvotes: 23

Views: 19798

Answers (1)

FuzzyAmi
FuzzyAmi

Reputation: 8119

I spent a good hour on this. Turns out the format for specifying boolean args is:

my_application -debug=false -another_boolean_param=boolean_value

and not as stated in the question. This is tricky: non boolean parameters do NOT require the "=" character.

Upvotes: 60

Related Questions