/
argparse.go
65 lines (63 loc) · 1.44 KB
/
argparse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import "strings"
// parseArg parses restic arguments and splits them by spaces or quoted parts
func parseArg(arg string) []string {
// res is the assembled result
var res []string
// sb is assembling the current argument
sb := strings.Builder{}
// isEscaped is true if the previous character was a backslash
isEscaped := false
// isQuoted is true if the current string is contained in double quotes
isQuoted := false
// iterate over each individual rune
for _, x := range arg {
if isEscaped {
// last character was a backslash
isEscaped = false
if x == '"' {
sb.WriteRune(x)
continue
} else if x == '\\' {
sb.WriteRune(x)
continue
} else {
// not a backslash, output the previously omitted backslash
sb.WriteRune('\\')
}
}
if x == '\\' {
// indicate backslash and check upcoming rune to decide on proceeding
isEscaped = true
continue
}
if x == '"' {
if isQuoted {
// quoted string is closed
isQuoted = false
if sb.Len() > 0 {
res = append(res, sb.String())
sb.Reset()
}
} else {
// start quoted string
isQuoted = true
}
continue
}
if x == ' ' && !isQuoted {
// space indentified as separator (not part of a quoted string)
if sb.Len() > 0 {
res = append(res, sb.String())
sb.Reset()
}
continue
}
// other character, just append
sb.WriteRune(x)
}
if sb.Len() > 0 {
res = append(res, sb.String())
}
return res
}