diff --git a/ip.go b/ip.go index 06b8bcb5..046f48b3 100644 --- a/ip.go +++ b/ip.go @@ -32,6 +32,9 @@ func (i *ipValue) Type() string { } func ipConv(sval string) (interface{}, error) { + if sval == "" || sval == "" { + return net.IP(nil), nil + } ip := net.ParseIP(sval) if ip != nil { return ip, nil diff --git a/ip_test.go b/ip_test.go index 7a5da106..8f11ac46 100644 --- a/ip_test.go +++ b/ip_test.go @@ -61,3 +61,20 @@ func TestIP(t *testing.T) { } } } + +func TestIPNilDefault(t *testing.T) { + var addr net.IP + f := NewFlagSet("test", ContinueOnError) + f.IPVar(&addr, "address", nil, "IP Address") + + ip, err := f.GetIP("address") + if err != nil { + t.Fatalf("expected nil default IP to round-trip without error, got %v", err) + } + if ip != nil { + t.Fatalf("expected nil default IP, got %v", ip) + } + if addr != nil { + t.Fatalf("expected bound IP variable to stay nil, got %v", addr) + } +}