pflag icon indicating copy to clipboard operation
pflag copied to clipboard

Support for nil default value in IP flag

Open Maximilan4 opened this issue 2 years ago • 1 comments

I have some flag definition code in cobra:

listCmd.Flags().IP("ip", nil, "--ip")

And then i running this:

ip, err := flags.GetIP("ip")

I got an error: invalid string being converted to IP address: <nil> Same result with this definition:

listCmd.Flags().IP("ip", net.IP{}, "--ip")

In my logic, flag is optional, and i dont want to work with net.Parse("0.0.0.0") and string values.

Updated nil as default value works thought IPVar, but wanna minimize package level variables for each command.

var ip  net.IP
listCmd.Flags().IPVar(&ip, "ip", nil, "--ip")

Maximilan4 avatar May 25 '22 15:05 Maximilan4

I have the same question. some times, I just want to keep some var null when users don't pass args.

lee3164 avatar Nov 30 '22 13:11 lee3164