Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow slice and map fields to accept newline delimited values #29

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ converted into the field's type. For example,
FlagSetFiller also includes support for []string fields.
Repetition of the argument appends to the slice and/or an argument value can contain a
comma-separated list of values.
comma or newline separated list of values.
For example:
Expand All @@ -126,7 +126,7 @@ The default tag's value is provided as a comma-separated list, such as
FlagSetFiller also includes support for map[string]string fields.
Each argument entry is a key=value and/or repetition of the arguments adds to the map or
multiple entries can be comma-separated in a single argument value.
multiple entries can be comma or newline separated in a single argument value.
For example:
Expand Down
8 changes: 6 additions & 2 deletions flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -658,7 +659,8 @@ func (s *strSliceVar) Set(val string) error {
}

func parseStringSlice(val string) []string {
return strings.Split(val, ",")
splitter := regexp.MustCompile("[\n,]")
return splitter.Split(val, -1)
}

type strToStrMapVar struct {
Expand Down Expand Up @@ -696,7 +698,9 @@ func (s strToStrMapVar) Set(val string) error {
func parseStringToStringMap(val string) map[string]string {
result := make(map[string]string)

pairs := strings.Split(val, ",")
splitter := regexp.MustCompile("[\n,]")

pairs := splitter.Split(val, -1)
for _, pair := range pairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 {
Expand Down
11 changes: 8 additions & 3 deletions flagset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,13 @@ func TestStringSlice(t *testing.T) {
"--no-default", "nd1",
"--no-default", "nd2",
"--no-default", "nd3,nd4",
"--no-default", "nd5\nnd6",
"--tag-default", "three",
"--tag-override", "three",
})
require.NoError(t, err)

assert.Equal(t, []string{"nd1", "nd2", "nd3", "nd4"}, config.NoDefault)
assert.Equal(t, []string{"nd1", "nd2", "nd3", "nd4", "nd5", "nd6"}, config.NoDefault)
assert.Equal(t, []string{"apple", "orange"}, config.InstanceDefault)
assert.Equal(t, []string{"one", "two", "three"}, config.TagDefault)
assert.Equal(t, []string{"three"}, config.TagOverride)
Expand Down Expand Up @@ -599,10 +600,14 @@ func TestStringToStringMap(t *testing.T) {
\(default (veggie=carrot,fruit=apple|fruit=apple,veggie=carrot)\)
`, buf.String())

err = flagset.Parse([]string{"--no-default", "k1=v1", "--no-default", "k2=v2,k3=v3"})
err = flagset.Parse([]string{"--no-default",
"k1=v1",
"--no-default",
"k2=v2,k3=v3\nk4=v4",
})
require.NoError(t, err)

assert.Equal(t, map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, config.NoDefault)
assert.Equal(t, map[string]string{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}, config.NoDefault)
assert.Equal(t, map[string]string{"fruit": "apple", "veggie": "carrot"}, config.TagDefault)
}

Expand Down