keep trailing space at commandline

This commit is contained in:
boombuler
2016-09-03 08:16:18 +02:00
parent ccfe08bc60
commit 8617ae5c1f
2 changed files with 12 additions and 6 deletions

View File

@@ -248,17 +248,20 @@ func SplitCommandArgs(input string) []string {
escape = false
curArg.WriteRune(r)
}
if curArg.Len() > 0 || len(result) == 0 {
result = append(result, curArg.String())
}
//if curArg.Len() > 0 || len(result) == 0 {
result = append(result, curArg.String())
// }
return result
}
// JoinCommandArgs joins multiple command arguments and quote the strings if needed.
func JoinCommandArgs(args ...string) string {
buf := new(bytes.Buffer)
first := true
for _, arg := range args {
if buf.Len() > 0 {
if first {
first = false
} else {
buf.WriteRune(' ')
}
if !strings.Contains(arg, " ") {

View File

@@ -79,15 +79,18 @@ func TestJoinAndSplitCommandArgs(t *testing.T) {
{[]string{`foo`}, `foo`},
{[]string{`foo\"bar`}, `foo\"bar`},
{[]string{``}, ``},
{[]string{`a`, ``}, `a `},
{[]string{``, ``, ``, ``}, ` `},
}
for i, test := range tests {
if result := JoinCommandArgs(test.Query...); test.Wanted != result {
t.Errorf("JoinCommandArgs failed at Test %d\nGot: %v", i, result)
t.Errorf("JoinCommandArgs failed at Test %d\nGot: %q", i, result)
}
if result := SplitCommandArgs(test.Wanted); !reflect.DeepEqual(test.Query, result) {
t.Errorf("SplitCommandArgs failed at Test %d\nGot: %v", i, result)
t.Errorf("SplitCommandArgs failed at Test %d\nGot: `%s`", i, result)
}
}
}