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 // determine the index of the first flag if we need to ignore them
if p.Metadata.IgnoreFlags { if p.Metadata.IgnoreFlags {
for i, arg := range extraArgs { for i, arg := range extraArgs {
if strings.HasPrefix(arg, "--") { if strings.HasPrefix(arg, "-") {
extraIndex = i extraIndex = i
break break
} }

@ -29,30 +29,54 @@ func TestPrepareCommand(t *testing.T) {
}, },
} }
testCases := []struct { testCases := []struct {
description string
argv []string argv []string
expect []string expect []string
ignoreFlags bool ignoreFlags bool
}{ }{
{ {
description: "no extra args",
argv: []string{},
expect: []string{"-n", "foo"},
},
{
description: "extra args, all long flags, keep flags",
argv: []string{"--debug", "--foo", "bar"}, argv: []string{"--debug", "--foo", "bar"},
expect: []string{"-n", "foo", "--debug", "--foo", "bar"}, expect: []string{"-n", "foo", "--debug", "--foo", "bar"},
}, },
{ {
argv: []string{}, description: "extra args, all long flags, ignore flags",
argv: []string{"--debug", "--foo", "bar"},
expect: []string{"-n", "foo"}, expect: []string{"-n", "foo"},
ignoreFlags: true,
}, },
{ {
argv: []string{"--debug", "--foo", "bar"}, 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"}, expect: []string{"-n", "foo"},
ignoreFlags: true, ignoreFlags: true,
}, },
{ {
argv: []string{"arg1", "arg2", "--debug", "--foo", "bar", "arg3"}, 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"}, expect: []string{"-n", "foo", "arg1", "arg2"},
ignoreFlags: true, 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 { for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
p.Metadata.IgnoreFlags = tc.ignoreFlags p.Metadata.IgnoreFlags = tc.ignoreFlags
cmd, args := p.PrepareCommand(tc.argv) cmd, args := p.PrepareCommand(tc.argv)
if cmd != "echo" { if cmd != "echo" {
@ -68,6 +92,7 @@ func TestPrepareCommand(t *testing.T) {
t.Errorf("Expected arg=%q, got %q", tc.expect[i], args[i]) t.Errorf("Expected arg=%q, got %q", tc.expect[i], args[i])
} }
} }
})
} }
} }

Loading…
Cancel
Save