-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.go
78 lines (67 loc) · 2.31 KB
/
regexp.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
package commander
import (
"regexp"
"strings"
)
// defineCommand define commands
func defineCommand(str string) string {
return regexp.MustCompile(`[A-Za-z0-9_-]+`).FindString(str)
}
// regexpCommand Regular screening out docopt commands
func regexpCommand(str string) []string {
return regexp.MustCompile(`[A-Za-z0-9_-]+`).FindAllString(
regexp.MustCompile(`^[A-Za-z0-9_|\(\)\s-]+`).FindString(str), -1)
}
// regexpArgument Regular screening out docopt arguments
func regexpArgument(str string) []string {
return regexp.MustCompile(`(?i:<|\[)[A-Za-z0-9_\[\]<>-]+\b(?i:>|])`).
FindAllString(str, -1)
}
// regexpOption Regular screening out docopt options
func regexpOption(str string) []string {
return regexp.MustCompile(`-{1,2}[A-Za-z0-9_-]+\b`).
FindAllString(regexp.MustCompile(`(<|\[)[A-Za-z0-9_\[\]<>-]+\b(>|])`).
ReplaceAllString(str, ""), -1)
}
//// containCommand str contain docopt command format
//func containCommand(str string) (ok bool) {
// ok, _ = regexp.MatchString(`[A-Za-z0-9_-]+`,
// regexp.MustCompile(`^[A-Za-z0-9_|\(\)\s-]+`).FindString(str))
// return
//}
//
//// containArgument str contain docopt argument format
//func containArgument(str string) (ok bool) {
// ok, _ = regexp.MatchString(`(?i:<|\[)[A-Za-z0-9_\[\]<>-]+\b(?i:>|])`, str)
// return
//}
//
//// containOption str contain docopt option format
//func containOption(str string) (ok bool) {
// ok, _ = regexp.MatchString(`-{1,2}[A-Za-z0-9_-]+\b`,
// regexp.MustCompile(`(<|\[)[A-Za-z0-9_\[\]<>-]+\b(>|])`).
// ReplaceAllString(str, ""))
// return
//}
//// isCommand str contain docopt command format
//func isCommand(str string) bool {
// return !isArgument(str) && !isOption(str)
//}
// isArgument str contain docopt command format
func isArgument(str string) (ok bool) {
ok, _ = regexp.MatchString(`^<[A-Za-z0-9_-]+>$`, str)
return
}
// isOption str contain docopt command format
func isOption(str string) (ok bool) {
ok, _ = regexp.MatchString(`^-{1,2}[A-Za-z0-9_-]+$`, str)
return
}
// replaceCommand replace name of command in usage with new name
func replaceCommand(usage, oldName, newName string) string {
return strings.Replace(usage, oldName, newName, 1)
}
// firstParameter find first parameter in usage
func firstParameter(usage string) string {
return regexp.MustCompile(`^([^\s=]+){1}`).FindString(strings.TrimSpace(usage))
}