Skip to content

Commit

Permalink
Allow slice and map fields to accept newline delimited values (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Oct 28, 2023
1 parent 4fe40c0 commit c0bd140
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
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

0 comments on commit c0bd140

Please sign in to comment.