From fb97335a13312831f89c8b88b898701c7c8233b9 Mon Sep 17 00:00:00 2001 From: Rick Date: Sat, 16 Sep 2017 12:05:53 +0100 Subject: [PATCH] Allow spaces after each comma in tags --- parse.go | 1 + parse_test.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/parse.go b/parse.go index ecbb7e4..ee10387 100644 --- a/parse.go +++ b/parse.go @@ -168,6 +168,7 @@ func NewParser(config Config, dests ...interface{}) (*Parser, error) { // Look at the tag if tag != "" { for _, key := range strings.Split(tag, ",") { + key = strings.TrimLeft(key, " ") var value string if pos := strings.Index(key, ":"); pos != -1 { value = key[pos+1:] diff --git a/parse_test.go b/parse_test.go index a646f2b..55f3fa6 100644 --- a/parse_test.go +++ b/parse_test.go @@ -131,7 +131,7 @@ func TestMixed(t *testing.T) { var args struct { Foo string `arg:"-f"` Bar int - Baz uint `arg:"positional"` + Baz uint `arg:"positional"` Ham bool Spam float32 } @@ -341,9 +341,9 @@ func TestMissingRequired(t *testing.T) { assert.Error(t, err) } -func TestMissingRequiredMultiplePositional(t *testing.T) { +func TestNonsenseKey(t *testing.T) { var args struct { - X []string `arg:"positional, required"` + X []string `arg:"positional, nonsense"` } err := parse("x", &args) assert.Error(t, err) @@ -821,3 +821,13 @@ func TestSeparatePositionalInterweaved(t *testing.T) { assert.Equal(t, "zzz", args.Pre) assert.Equal(t, []string{"post1", "post2", "post3"}, args.Post) } + +func TestSpacesAllowedInTags(t *testing.T) { + var args struct { + Foo []string `arg:"--foo, -f, separate, required, help:quite nice really"` + } + + err := parse("--foo one -f=two --foo=three -f four", &args) + require.NoError(t, err) + assert.Equal(t, []string{"one", "two", "three", "four"}, args.Foo) +}