Skip to content

Commit d68a2b8

Browse files
committed
Support empty arguments
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
1 parent 51d68c7 commit d68a2b8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

shellwords.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,17 @@ loop:
144144
}
145145
case '"':
146146
if !singleQuoted && !dollarQuote {
147+
if doubleQuoted && buf == "" {
148+
got = true
149+
}
147150
doubleQuoted = !doubleQuoted
148151
continue
149152
}
150153
case '\'':
151154
if !doubleQuoted && !dollarQuote {
155+
if singleQuoted && buf == "" {
156+
got = true
157+
}
152158
singleQuoted = !singleQuoted
153159
continue
154160
}

shellwords_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ func TestLastSpace(t *testing.T) {
6969
}
7070
}
7171

72+
func TestEmptyArgs(t *testing.T) {
73+
args, err := Parse(`foo "" bar ''`)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
if len(args) != 4 {
78+
t.Fatal("Should have three elements")
79+
}
80+
if args[0] != "foo" {
81+
t.Fatal("1st element should be `foo`")
82+
}
83+
if args[1] != "" {
84+
t.Fatal("2nd element should be empty")
85+
}
86+
if args[2] != "bar" {
87+
t.Fatal("3rd element should be `bar`")
88+
}
89+
if args[3] != "" {
90+
t.Fatal("4th element should be empty")
91+
}
92+
}
93+
7294
func TestShellRun(t *testing.T) {
7395
dir, err := os.Getwd()
7496
if err != nil {

0 commit comments

Comments
 (0)