Merge pull request #57 from rickb777/master

Allow spaces after each comma in tags
This commit is contained in:
Alex Flint 2017-09-27 14:56:33 -07:00 committed by GitHub
commit 398a01ebab
2 changed files with 14 additions and 3 deletions

View File

@ -168,6 +168,7 @@ func NewParser(config Config, dests ...interface{}) (*Parser, error) {
// Look at the tag // Look at the tag
if tag != "" { if tag != "" {
for _, key := range strings.Split(tag, ",") { for _, key := range strings.Split(tag, ",") {
key = strings.TrimLeft(key, " ")
var value string var value string
if pos := strings.Index(key, ":"); pos != -1 { if pos := strings.Index(key, ":"); pos != -1 {
value = key[pos+1:] value = key[pos+1:]

View File

@ -341,9 +341,9 @@ func TestMissingRequired(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
} }
func TestMissingRequiredMultiplePositional(t *testing.T) { func TestNonsenseKey(t *testing.T) {
var args struct { var args struct {
X []string `arg:"positional, required"` X []string `arg:"positional, nonsense"`
} }
err := parse("x", &args) err := parse("x", &args)
assert.Error(t, err) assert.Error(t, err)
@ -821,3 +821,13 @@ func TestSeparatePositionalInterweaved(t *testing.T) {
assert.Equal(t, "zzz", args.Pre) assert.Equal(t, "zzz", args.Pre)
assert.Equal(t, []string{"post1", "post2", "post3"}, args.Post) 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)
}