Specify number of splits to make for command arguments
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2022-09-06 10:43:30 -05:00
parent 4068a4ff06
commit 139b32094e
4 changed files with 102 additions and 32 deletions

View File

@@ -99,24 +99,43 @@ func ContainsCommand(s, prefix, cmd string) bool {
return false
}
func SplitCommandAndArg(s, prefix string) (cmd string, args []string) {
func SplitCommandAndArg(s, prefix string) (cmd string, arg string) {
s = strings.TrimSpace(s)
if !strings.HasPrefix(s, prefix) {
return
}
x := strings.Split(s, " ")
// remove the command prefix
s = s[len(prefix):]
if len(x) > 1 {
args = x[1:]
}
// multiple assignment trick
cmd, arg = func() (string, string) {
x := strings.SplitN(s, " ", 2)
if len(x) > 1 {
return x[0], x[1]
}
return x[0], ""
}()
cmd = x[0]
if strings.Index(s, prefix) == 0 {
cmd = cmd[len(prefix):]
}
return cmd, args
return cmd, arg
}
func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) {
cmd, arg := SplitCommandAndArg(s, prefix)
if n == 0 {
return cmd, strings.Split(arg, " ")
}
return cmd, strings.SplitN(arg, " ", n)
}
func SplitArgs(s string, n int) (args []string) {
if n > 0 {
args = strings.SplitN(s, " ", n)
} else {
args = strings.Split(s, " ")
}
return
}

View File

@@ -57,20 +57,44 @@ func TestHasCommandCommand(t *testing.T) {
}
}
func TestSplitComandAndArgs(t *testing.T) {
func TestSplitCommandAndArg(t *testing.T) {
tables := []struct {
s string
prefix string
wantCmd string
wantArgs []string
s string
prefix string
wantCmd string
wantArg string
}{
{"!command x y", "!", "command", []string{"x", "y"}},
{"!command", "!", "command", []string(nil)},
{"hey man", "!", "", []string(nil)},
{"!command x y", "!", "command", "x y"},
{"!command", "!", "command", ""},
{"hey man", "!", "", ""},
}
for _, table := range tables {
gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix)
gotCmd, gotArg := SplitCommandAndArg(table.s, table.prefix)
if gotCmd != table.wantCmd {
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
}
if gotArg != table.wantArg {
t.Errorf("got: %+v, want: %+v", gotArg, table.wantArg)
}
}
}
func TestSplitCommandAndArgs(t *testing.T) {
tables := []struct {
s string
prefix string
n int
wantCmd string
wantArgs []string
}{
{"!command x y", "!", 2, "command", []string{"x", "y"}},
{"!command x y z", "!", 2, "command", []string{"x", "y z"}},
{"!command", "!", 1, "command", []string{""}},
{"hey man", "!", 1, "", []string{""}},
}
for _, table := range tables {
gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix, table.n)
if gotCmd != table.wantCmd {
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
}
@@ -79,3 +103,22 @@ func TestSplitComandAndArgs(t *testing.T) {
}
}
}
func TestSplitArgs(t *testing.T) {
tables := []struct {
s string
n int
want []string
}{
{"a b c", 0, []string{"a", "b", "c"}},
{"a b c", 1, []string{"a b c"}},
{"a b c", 2, []string{"a", "b c"}},
{"a b c", 3, []string{"a", "b", "c"}},
{"a b c", 4, []string{"a", "b", "c"}},
}
for _, table := range tables {
if got, want := SplitArgs(table.s, table.n), table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}