-
Notifications
You must be signed in to change notification settings - Fork 191
/
util.go
179 lines (151 loc) · 3.97 KB
/
util.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cflag
import (
"flag"
"reflect"
"regexp"
"sort"
"strings"
"github.com/gookit/color"
)
const (
// RegGoodName match a good option, argument name
RegGoodName = `^[a-zA-Z][\w-]*$`
)
var (
// GoodName good name for option and argument
goodName = regexp.MustCompile(RegGoodName)
)
// IsGoodName check
func IsGoodName(name string) bool {
return goodName.MatchString(name)
}
// IsZeroValue determines whether the string represents the zero
// value for a flag.
//
// from flag.isZeroValue() and more return the second arg for check is string.
func IsZeroValue(opt *flag.Flag, value string) (bool, bool) {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(opt.Value)
var z reflect.Value
if typ.Kind() == reflect.Ptr {
z = reflect.New(typ.Elem())
} else {
z = reflect.Zero(typ)
}
return value == z.Interface().(flag.Value).String(), z.Kind() == reflect.String
}
// AddPrefix for render flag options help
func AddPrefix(name string) string {
if len(name) > 1 {
return "--" + name
}
return "-" + name
}
// AddPrefixes for render flag options help, name will first add.
func AddPrefixes(name string, shorts []string) string {
return AddPrefixes2(name, shorts, false)
}
// AddPrefixes2 for render flag options help, can custom name add position.
func AddPrefixes2(name string, shorts []string, nameAtEnd bool) string {
shortLn := len(shorts)
if shortLn == 0 {
return AddPrefix(name)
}
sort.Strings(shorts)
withPfx := make([]string, 0, shortLn+1)
if !nameAtEnd {
withPfx = append(withPfx, AddPrefix(name))
}
// append shorts
for _, short := range shorts {
withPfx = append(withPfx, AddPrefix(short))
}
if nameAtEnd {
withPfx = append(withPfx, AddPrefix(name))
}
return strings.Join(withPfx, ", ")
}
// SplitShortcut string to []string
func SplitShortcut(shortcut string) []string {
return FilterNames(strings.Split(shortcut, ","))
}
// FilterNames for option names, will clear there are: "-+= "
func FilterNames(names []string) []string {
filtered := make([]string, 0, len(names))
for _, sub := range names {
if sub = strings.TrimSpace(sub); sub != "" {
sub = strings.Trim(sub, "-+= ")
if sub != "" {
filtered = append(filtered, sub)
}
}
}
return filtered
}
// IsFlagHelpErr check
func IsFlagHelpErr(err error) bool {
if err == nil {
return false
}
return err == flag.ErrHelp
}
// regex: "`[\w ]+`"
// regex: "`.+`"
var codeReg = regexp.MustCompile("`" + `.+` + "`")
// WrapColorForCode convert "hello `keywords`" to "hello <mga>keywords</>"
func WrapColorForCode(s string) string {
if !strings.ContainsRune(s, '`') {
return s
}
return codeReg.ReplaceAllStringFunc(s, func(code string) string {
code = strings.Trim(code, "`")
return color.WrapTag(code, "mga")
})
}
// ParseStopMark string
const ParseStopMark = "--"
// ReplaceShorts replace shorts to full option. will stop on ParseStopMark
//
// For example:
//
// eg: '-f' -> '--file'.
// eg: '-n=tom' -> '--name=tom'.
func ReplaceShorts(args []string, shortsMap map[string]string) []string {
if len(args) == 0 {
return args
}
fmtArgs := make([]string, 0, len(args))
for i, arg := range args {
if arg == "" || arg[0] != '-' || len(arg) > 64 {
fmtArgs = append(fmtArgs, arg)
continue
}
if arg == ParseStopMark {
fmtArgs = append(fmtArgs, args[i:]...)
break
}
var handled bool
for short, name := range shortsMap {
sOpt := AddPrefix(short)
// is short name, replace to full opt. eg: '-f' -> '--file'
if arg == sOpt {
handled = true
fmtArgs = append(fmtArgs, AddPrefix(name))
break
}
// special, use '=' split value. eg: '-n=tom' -> '--name=tom'
if strings.HasPrefix(arg, sOpt+"=") {
handled = true
fullOpt := AddPrefix(name)
fmtArgs = append(fmtArgs, fullOpt+arg[len(sOpt):])
break
}
}
if !handled {
fmtArgs = append(fmtArgs, arg)
}
}
return fmtArgs
}