-
Notifications
You must be signed in to change notification settings - Fork 55
/
optionSet.go
217 lines (180 loc) · 4.73 KB
/
optionSet.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package goNixArgParser
import (
"errors"
"io"
"os"
"strings"
)
func NewOptionSet(
mergeFlagPrefix string,
restsSigns []string,
groupSeps []string,
assignSigns []string,
undefFlagPrefixes []string,
) *OptionSet {
s := &OptionSet{
mergeFlagPrefix: mergeFlagPrefix,
restsSigns: restsSigns,
groupSeps: groupSeps,
assignSigns: assignSigns,
undefFlagPrefixes: undefFlagPrefixes,
options: []*Option{},
keyOptionMap: map[string]*Option{},
flagOptionMap: map[string]*Option{},
nameFlagMap: map[string]*Flag{},
keyEnvMap: map[string][]string{},
keyDefaultMap: map[string][]string{},
}
return s
}
func (s *OptionSet) MergeFlagPrefix() string {
return s.mergeFlagPrefix
}
func (s *OptionSet) RestsSigns() []string {
return s.restsSigns
}
func (s *OptionSet) GroupSeps() []string {
return s.groupSeps
}
func (s *OptionSet) UndefFlagPrefixes() []string {
return s.undefFlagPrefixes
}
func NewSimpleOptionSet() *OptionSet {
return NewOptionSet("-", []string{"--"}, []string{",,"}, []string{"="}, []string{"-"})
}
func (s *OptionSet) isRestSign(input string) bool {
for _, sign := range s.restsSigns {
if input == sign {
return true
}
}
return false
}
func (s *OptionSet) isGroupSep(input string) bool {
for _, sep := range s.groupSeps {
if input == sep {
return true
}
}
return false
}
func (s *OptionSet) isUdefFlag(input string) bool {
for _, prefix := range s.undefFlagPrefixes {
if len(input) > len(prefix) && strings.HasPrefix(input, prefix) {
return true
}
}
return false
}
func (s *OptionSet) findFlagByPrefix(prefix string) (flag *Flag, ambiguous bool) {
if !s.hasPrefixMatch {
return
}
prefixLen := len(prefix)
if prefixLen == 0 {
return
}
var matched *Flag
for _, opt := range s.options {
for _, flag := range opt.Flags {
if prefixLen >= flag.prefixMatchLen && strings.HasPrefix(flag.Name, prefix) {
if matched != nil { // found more than 1 match, not unique match
return nil, true
}
matched = flag
}
}
}
return matched, false
}
func (s *OptionSet) Add(opt Option) error {
// verify
if len(opt.Key) == 0 {
return errors.New("key is empty")
}
if s.keyOptionMap[opt.Key] != nil {
return errors.New("key '" + opt.Key + "' already exists")
}
for _, flag := range opt.Flags {
flagName := flag.Name
if len(flagName) == 0 {
return errors.New("flag name is empty")
}
if s.nameFlagMap[flagName] != nil {
return errors.New("flag '" + flagName + "' already exists")
}
}
if !opt.AcceptValue && len(opt.DefaultValues) > 0 {
opt.DefaultValues = nil
}
option := &opt
// append
s.options = append(s.options, option)
// redundant - flag summaries, maps
s.keyOptionMap[option.Key] = option
for _, flag := range option.Flags {
if flag.canMerge {
s.hasCanMerge = true
}
if flag.canConcatAssign {
s.hasCanConcatAssign = true
}
if flag.prefixMatchLen > 0 {
s.hasPrefixMatch = true
}
flagName := flag.Name
s.flagOptionMap[flagName] = option
s.nameFlagMap[flagName] = flag
}
// redundant - env maps
for _, envVar := range option.EnvVars {
if len(envVar) == 0 {
continue
}
envValue, hasEnv := os.LookupEnv(envVar)
if !hasEnv {
continue
}
if len(envValue) > 0 {
if option.MultiValues {
s.keyEnvMap[option.Key] = option.splitValues(envValue)
} else {
s.keyEnvMap[option.Key] = []string{envValue}
}
} else {
s.keyEnvMap[option.Key] = []string{}
}
}
// redundant - default maps
if len(option.DefaultValues) > 0 {
s.keyDefaultMap[option.Key] = option.DefaultValues
}
return nil
}
func (s *OptionSet) AddFlag(key, flag, envVar, summary string) error {
return s.Add(NewFlagOption(key, flag, envVar, summary))
}
func (s *OptionSet) AddFlags(key string, flags []string, envVar, summary string) error {
return s.Add(NewFlagsOption(key, flags, envVar, summary))
}
func (s *OptionSet) AddFlagValue(key, flag, envVar, defaultValue, summary string) error {
return s.Add(NewFlagValueOption(key, flag, envVar, defaultValue, summary))
}
func (s *OptionSet) AddFlagValues(key, flag, envVar string, defaultValues []string, summary string) error {
return s.Add(NewFlagValuesOption(key, flag, envVar, defaultValues, summary))
}
func (s *OptionSet) AddFlagsValue(key string, flags []string, envVar, defaultValue, summary string) error {
return s.Add(NewFlagsValueOption(key, flags, envVar, defaultValue, summary))
}
func (s *OptionSet) AddFlagsValues(key string, flags []string, envVar string, defaultValues []string, summary string) error {
return s.Add(NewFlagsValuesOption(key, flags, envVar, defaultValues, summary))
}
func (s *OptionSet) OutputHelp(w io.Writer) {
newline := []byte{'\n'}
for _, opt := range s.options {
if !opt.Hidden {
opt.OutputHelp(w)
w.Write(newline)
}
}
}