Skip to content

fix: allow empty string to reset StringToString flag#491

Open
happysnaker wants to merge 1 commit into
spf13:masterfrom
happysnaker:fix-stringtostring-empty-value
Open

fix: allow empty string to reset StringToString flag#491
happysnaker wants to merge 1 commit into
spf13:masterfrom
happysnaker:fix-stringtostring-empty-value

Conversation

@happysnaker

Copy link
Copy Markdown

Summary

Fixes #312

Currently, passing an empty string to a StringToString flag (e.g., --flag="") returns an error because the parser expects a key=value format. This makes it impossible to override a non-empty default map with an empty one.

Changes

Allow an empty string value to reset the flag to an empty map. When val == "", the existing map entries are cleared and changed is set to true.

Before

fs := pflag.NewFlagSet("test", pflag.ExitOnError)
fs.StringToString("x", map[string]string{"a":"1"}, "")
fs.Set("x", "")  // Error: must be formatted as key=value

After

fs.Set("x", "")  // OK: map is now empty

Currently, passing an empty string to a StringToString flag returns
an error. This makes it impossible to override a non-empty default
map with an empty one.

This change allows an empty string value to reset the flag to an
empty map.

Fixes spf13#312
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.

Allow an empty value for StringToString

1 participant