-
Notifications
You must be signed in to change notification settings - Fork 58
/
helpers.go
63 lines (56 loc) · 1.35 KB
/
helpers.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
package ff
import (
"fmt"
"strings"
"unicode/utf8"
)
func newFlagError(f Flag, err error) error {
return fmt.Errorf("%s: %w", getNameString(f), err)
}
func isValidShortName(short rune) bool {
var (
isZero = short == 0
isError = short == utf8.RuneError
isValid = !isZero && !isError
)
return isValid
}
var badStuff = strings.Join([]string{
string(rune(0x00)), // zero rune
string([]byte{0x00}), // zero/NUL byte
` `, // space
"\t\n\v\f\r", // control whitespace
string(rune(0x85)), // unicode whitespace
string(rune(0xA0)), // unicode whitespace
`"'`, // quotes
"`", // backtick
`\`, // backslash
}, "")
func isValidLongName(long string) bool {
var (
isEmpty = long == ""
hasBadStuff = strings.ContainsAny(long, badStuff)
isValid = !isEmpty && !hasBadStuff
)
return isValid
}
func getNameStrings(f Flag) []string {
var names []string
if short, ok := f.GetShortName(); ok {
names = append(names, string(short))
}
if long, ok := f.GetLongName(); ok {
names = append(names, long)
}
return names
}
func getNameString(f Flag) string {
var names []string
if short, ok := f.GetShortName(); ok {
names = append(names, "-"+string(short))
}
if long, ok := f.GetLongName(); ok {
names = append(names, "--"+long)
}
return strings.Join(names, ", ")
}