-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
83 lines (79 loc) · 1.5 KB
/
parse.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package utils
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
func ParsePort(portString string) []int {
ports := []int{}
m := make(map[int]struct{})
for _, f1 := range strings.Split(portString, ",") {
if strings.Contains(f1, "-") {
f2 := strings.Split(f1, "-")
if len(f2) != 2 {
continue
}
s, err := strconv.Atoi(strings.TrimSpace(f2[0]))
if err != nil {
continue
}
e, err := strconv.Atoi(strings.TrimSpace(f2[1]))
if err != nil {
continue
}
if s >= e || s <= 0 {
continue
}
for ; s < e; s++ {
if _, ok := m[s]; ok {
continue
}
ports = append(ports, s)
m[s] = struct{}{}
}
} else {
port, err := strconv.Atoi(strings.TrimSpace(f1))
if err != nil {
continue
}
if _, ok := m[port]; ok {
continue
}
ports = append(ports, port)
m[port] = struct{}{}
}
}
return ports
}
func ParseCommandArgs(s string) []string {
rd := bufio.NewReader(strings.NewReader(s))
fields := []string{}
var flag byte
for {
field, err := rd.ReadString(32)
if err != nil {
if err == io.EOF && len(field) > 0 {
fields = append(fields, strings.TrimSpace(field))
}
break
}
if field == " " {
continue
}
if strings.Contains(field, string(34)) { //双引号
flag = 34
}
if flag != 0 {
s, err := rd.ReadString(flag)
if err != nil {
break
}
field = strings.Trim(fmt.Sprintf("%s %s", field, s), string(34))
flag = 0
}
fields = append(fields, strings.TrimSpace(field))
}
return fields
}