4 ms·
> no way to know whether the flag was given by the user or the default value was assumed implicitly I'm struggling to find a need for this. Flags define behavi
by spyspy 9y ago
> no way to know whether the flag was given by the user or the default value was assumed implicitly
I'm struggling to find a need for this. Flags define behavior, who cares if the behavior is explicit or implied from inaction?
- openasocket 9y agoIt matters if there's no way to have a flag without a default. If your flag doesn't have a clear "user didn't supply anything" default it can be annoying
- ecnahc515 9y agoIt's always possible to define a custom flag type to handle this. It's definitely what I would consider niche however, since usually you don't care about set/unset just whatever the value is, default or not.
- spyspy 9y agoI've done something like this before: > id = flag.Int64("id", -1, "ID of the thing I want") Obviously that doesn't work for booleans, but you could take the boolean input as a string and then use strconv.ParseBool
- sethammons 9y agoI just ran into this for the first time in many years of Go usage. I wrote a command line utility for some code gen. If a flag was left off, I needed to prompt the user for an explicit value. For Boolean flags, this was not doable. I had to create a type that matched the flagset interface and use flag.Var for parsing it.