-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
flag.go
146 lines (134 loc) · 3.57 KB
/
flag.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
package spec
import (
"fmt"
"regexp"
"strings"
"github.com/spf13/pflag"
)
type flag struct {
longhand string
shorthand string
usage string
slice bool
optarg bool
value bool
nameAsShorthand bool
hidden bool
required bool
}
func parseFlag(s, usage string) (*flag, error) {
r := regexp.MustCompile(`^(?P<shorthand>-[^-][^ =*?&!]*)?(, )?(?P<longhand>-[-]?[^ =*?&!]*)?(?P<modifier>[=*?&!]*)$`)
if !r.MatchString(s) {
return nil, fmt.Errorf("flag syntax invalid: %v", s)
}
matches := findNamedMatches(r, s)
f := &flag{}
f.longhand = strings.TrimLeft(matches["longhand"], "-")
f.shorthand = strings.TrimPrefix(matches["shorthand"], "-")
f.nameAsShorthand = (matches["longhand"] != "" && !strings.HasPrefix(matches["longhand"], "--"))
f.usage = usage
f.slice = strings.Contains(matches["modifier"], "*")
f.optarg = strings.Contains(matches["modifier"], "?")
f.value = f.optarg || strings.Contains(matches["modifier"], "=")
f.hidden = strings.Contains(matches["modifier"], "&")
f.required = strings.Contains(matches["modifier"], "!")
if f.longhand == "" && f.shorthand == "" {
return nil, fmt.Errorf("malformed flag: '%v'", s)
}
return f, nil
}
func (f flag) addTo(fset *pflag.FlagSet) error {
fs := flagSet{fset}
if len(f.shorthand) > 1 && !fs.IsFork() {
return fmt.Errorf("long shorthand only supported with carapace-sh/carapace-pflag: %v", f.shorthand)
}
if f.longhand == "" && !fs.IsFork() {
return fmt.Errorf("shorthand-only only supported with carapace-sh/carapace-pflag: %v", f.shorthand)
}
if f.longhand != "" && f.shorthand != "" {
if f.value {
if f.slice {
if f.nameAsShorthand {
fs.StringSliceN(f.longhand, f.shorthand, []string{}, f.usage)
} else {
fs.StringSliceP(f.longhand, f.shorthand, []string{}, f.usage)
}
} else {
if f.nameAsShorthand {
fs.StringN(f.longhand, f.shorthand, "", f.usage)
} else {
fs.StringP(f.longhand, f.shorthand, "", f.usage)
}
}
} else {
if f.slice {
if f.nameAsShorthand {
fs.CountN(f.longhand, f.shorthand, f.usage)
} else {
fs.CountP(f.longhand, f.shorthand, f.usage)
}
} else {
if f.nameAsShorthand {
fs.BoolN(f.longhand, f.shorthand, false, f.usage)
} else {
fs.BoolP(f.longhand, f.shorthand, false, f.usage)
}
}
}
} else if f.longhand != "" {
if f.value {
if f.slice {
if f.nameAsShorthand {
fs.StringSliceS(f.longhand, f.longhand, []string{}, f.usage)
} else {
fs.StringSlice(f.longhand, []string{}, f.usage)
}
} else {
if f.nameAsShorthand {
fs.StringS(f.longhand, f.longhand, "", f.usage)
} else {
fs.String(f.longhand, "", f.usage)
}
}
} else {
if f.slice {
if f.nameAsShorthand {
fs.CountS(f.longhand, f.longhand, f.usage)
} else {
fs.Count(f.longhand, f.usage)
}
} else {
if !f.nameAsShorthand {
fs.Bool(f.longhand, false, f.usage)
} else {
fs.BoolS(f.longhand, f.longhand, false, f.usage)
}
}
}
} else if f.shorthand != "" {
if f.value {
if f.slice {
fs.StringSliceS(f.shorthand, f.shorthand, []string{}, f.usage)
} else {
fs.StringS(f.shorthand, f.shorthand, "", f.usage)
}
} else {
if f.slice {
fs.CountS(f.shorthand, f.shorthand, f.usage)
} else {
fs.BoolS(f.shorthand, f.shorthand, false, f.usage)
}
}
}
if f.optarg {
if f.longhand != "" {
fs.Lookup(f.longhand).NoOptDefVal = " "
} else {
fs.Lookup(f.shorthand).NoOptDefVal = " "
}
}
if f.hidden {
fs.Lookup(f.longhand).Hidden = f.hidden
}
return nil
}