1
0
mirror of https://github.com/ericchiang/pup synced 2024-11-28 02:48:16 +00:00

selectors can now be quoted

This commit is contained in:
ericchiang 2014-09-28 12:17:48 -04:00
parent e380e91ba3
commit 377f8f1b77

42
main.go
View File

@ -113,9 +113,49 @@ func ProcessFlags(cmds []string) []string {
return nonFlagCmds[:n] return nonFlagCmds[:n]
} }
// Split a string while ignoring strings.
func QuoteSplit(input string) []string {
last := 0
split := []string{}
inQuote := false
quoteChar := ' '
escapeNext := false
for i, c := range input {
if escapeNext {
escapeNext = false
continue
}
switch c {
case ' ':
if !inQuote {
if last < i-1 {
split = append(split, input[last:i])
}
last = i + 1
}
case '"', '\'':
if inQuote {
if c == quoteChar {
inQuote = false
}
} else {
inQuote = true
quoteChar = c
}
case '\\':
escapeNext = true
}
}
if last < len(input) {
split = append(split, input[last:len(input)])
}
return split
}
// pup // pup
func main() { func main() {
cmds := ProcessFlags(os.Args[1:]) args := QuoteSplit(strings.Join(os.Args[1:], " "))
cmds := ProcessFlags(args)
cr, err := charset.NewReader(inputStream, "") cr, err := charset.NewReader(inputStream, "")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())