-
Notifications
You must be signed in to change notification settings - Fork 66
Description
(First, thanks for ff.) I'm using v4, and I have some questions about how ff.FlagSets parse long flags that use =. After looking at the code, I'm not sure whether I'm seeing a bug or just a feature that I didn't expect.
- Boolean flags that are passed as
--flagname=(that is, with a long flag and no value after=) are parsed as true. Based on this comment, you may be doing this deliberately, but I'm inclined to think that this is a bug resulting from how you handle=earlier inparseLongFlag. - All other flag types that omit a value after
=consume the nextargas their value. This can have very surprising results for users who mistakenly forget a value after=. Consider the following.
fs := ff.NewFlagSet("myprogram")
debug := fs.Bool('d', "debug", "log debug information")
file := fs.String('f', "file", "", "file to frobnicate")
if err := fs.Parse([]string{"--file=", "--debug="}); err != nil {
panic(err)
}
fmt.Printf("file=%q\n", *file)
fmt.Printf("debug=%v\n", *debug)
fmt.Printf("args = %+v", fs.GetArgs())
// Output:
// file="--debug="
// debug=false
// args = []By way of comparison, stdlib's flag treats all empty values after = as errors except for string flags. (Presumably because they only return an error if a strconv conversion fails, and there is no conversion in the case of string values. An empty value == "".) This strikes me as the right approach (maybe even string flags should fail here). Leaving out a value is presumably user error, and parsing should fail and stop. (Alternatively, the treatment of empty values with = should be documented.)
If this is a bug, I think I have a fix, and I'm happy to make a PR. Let me know what you think.