short flags

pull/3268/head
Federico Gimenez 8 years ago
parent 3fdd3efd0d
commit 5465f6b059

@ -109,7 +109,7 @@ func (p *Plugin) PrepareCommand(extraArgs []string) (string, []string) {
// determine the index of the first flag if we need to ignore them
if p.Metadata.IgnoreFlags {
for i, arg := range extraArgs {
if strings.HasPrefix(arg, "--") {
if strings.HasPrefix(arg, "-") {
extraIndex = i
break
}

@ -29,45 +29,70 @@ func TestPrepareCommand(t *testing.T) {
},
}
testCases := []struct {
description string
argv []string
expect []string
ignoreFlags bool
}{
{
argv: []string{"--debug", "--foo", "bar"},
expect: []string{"-n", "foo", "--debug", "--foo", "bar"},
description: "no extra args",
argv: []string{},
expect: []string{"-n", "foo"},
},
{
argv: []string{},
expect: []string{"-n", "foo"},
description: "extra args, all long flags, keep flags",
argv: []string{"--debug", "--foo", "bar"},
expect: []string{"-n", "foo", "--debug", "--foo", "bar"},
},
{
description: "extra args, all long flags, ignore flags",
argv: []string{"--debug", "--foo", "bar"},
expect: []string{"-n", "foo"},
ignoreFlags: true,
},
{
description: "extra args, arguments, long flags, ignore flags",
argv: []string{"arg1", "arg2", "--debug", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo", "arg1", "arg2"},
ignoreFlags: true,
},
{
description: "extra args, short (first) and long flags, ignore flags",
argv: []string{"-s", "--debug", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo"},
ignoreFlags: true,
},
{
description: "extra args, arguments, short (first) and long flags, ignore flags",
argv: []string{"arg1", "arg2", "-d", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo", "arg1", "arg2"},
ignoreFlags: true,
},
{
description: "extra args, long (first) and short flags, ignore flags",
argv: []string{"--debug", "-s", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo"},
ignoreFlags: true,
},
}
for _, tc := range testCases {
p.Metadata.IgnoreFlags = tc.ignoreFlags
cmd, args := p.PrepareCommand(tc.argv)
if cmd != "echo" {
t.Errorf("Expected echo, got %q", cmd)
}
if l, le := len(args), len(tc.expect); l != le {
t.Errorf("expected %d args, got %d", le, l)
}
for i := 0; i < len(args); i++ {
if tc.expect[i] != args[i] {
t.Errorf("Expected arg=%q, got %q", tc.expect[i], args[i])
t.Run(tc.description, func(t *testing.T) {
p.Metadata.IgnoreFlags = tc.ignoreFlags
cmd, args := p.PrepareCommand(tc.argv)
if cmd != "echo" {
t.Errorf("Expected echo, got %q", cmd)
}
if l, le := len(args), len(tc.expect); l != le {
t.Errorf("expected %d args, got %d", le, l)
}
for i := 0; i < len(args); i++ {
if tc.expect[i] != args[i] {
t.Errorf("Expected arg=%q, got %q", tc.expect[i], args[i])
}
}
}
})
}
}

Loading…
Cancel
Save