Skip to content

ip: stop GetIP erroring when the IP flag has a nil default#478

Open
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/ip-nil-default
Open

ip: stop GetIP erroring when the IP flag has a nil default#478
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/ip-nil-default

Conversation

@c-tonneslan

Copy link
Copy Markdown

Reporter's case from #351:

fs.IP("ip", nil, "--ip")
ip, err := fs.GetIP("ip")
// err = invalid string being converted to IP address: <nil>

The flag's underlying value is a nil net.IP. When GetIP runs, getFlagType round-trips through Value.String(), and net.IP(nil).String() returns the literal "<nil>". ipConv then hands that to net.ParseIP, which rejects it.

Set("") already short-circuits without storing anything, so an empty value is the existing "no IP" sentinel. Mirror that on the read side: when ipConv sees "" or "<nil>", return (net.IP(nil), nil) instead of erroring.

The fix is one branch in ipConv. IPVar(&p, "ip", nil, ...) works the same way.

Regression test covers both IP and IPVar with a nil default. Fails on master, passes here. go test ./... is clean.

Fixes #351.

IP("ip", nil, "...") followed by GetIP("ip") returned
"invalid string being converted to IP address: <nil>" instead of
(nil, nil). The flag's stored value is a nil net.IP, which
net.IP.String renders as "<nil>", so ipConv saw that literal string
and net.ParseIP rejected it.

Treat "" and "<nil>" as "no IP" in ipConv, matching Set's empty-input
behaviour, so a nil default round-trips cleanly through GetIP.

Fixes spf13#351

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
@CLAassistant

CLAassistant commented May 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@tomasaschan

Copy link
Copy Markdown
Collaborator

@c-tonneslan Thanks for the contribution!

Please sign the CLA, and hit recheck on the bot comment above if it doesn't automatically detect that you did so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for nil default value in IP flag

3 participants