alecthomas / kingpin

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser
MIT License
3.48k stars 272 forks source link

Bad boolean Flag value is interpreted as Argument #322

Closed dbenque closed 3 years ago

dbenque commented 3 years ago

Using boolean Flag, and trying to assign a value to it is not reported as an error, instead the value is interpreted as an Argument of the program:

func main() {
    var (
        app = kingpin.New(filepath.Base(os.Args[0]), "My App.").DefaultEnvars()
        testBool = app.Flag("test-bool", "test-bool").Bool()
        //args = app.Arg("my args", "arg list").Required().Strings()
    )
    kingpin.MustParse(app.Parse(os.Args[1:]))
    fmt.Printf("test-bool=%s\n",strconv.FormatBool(*testBool))
    //fmt.Printf("args=%s\n",*args)
}

This would report an error if you run it (expected): go run main.go --test-bool=false --> main: error: unexpected false, try --help

Now if you remove the comments to run the program with Arguments, here is what you get: go run main.go --test-bool=false myArg

test-bool=true
args=[false myArg]

Not only the list of arguments contains the string value false but the boolean is set to true.

alecthomas commented 3 years ago

Kingpin doesn't support this approach, you can use --no-test-bool to negate a flag.