-
Notifications
You must be signed in to change notification settings - Fork 4
/
actions.go
247 lines (225 loc) · 6.73 KB
/
actions.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package argparse
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// Action is type to represent a callable function which will operate on a parser,
// a option, and an array of argument strings.
type Action func(*Parser, *Option, ...string) ([]string, error)
// Store will attempt to store the appropriate number of arguments for the option,
// (if any), into the parser. Remaining arguments & any errors are returned.
func Store(p *Parser, f *Option, args ...string) ([]string, error) {
// If we are not expecting any arguments, panic!
if f.ArgNum == "0" {
panic(fmt.Sprintf("option '%s' must expect at least one argument", f.DisplayName()))
} else if f.ArgNum == "?" {
if len(args) > 0 {
if err := ValidateChoice(*f, args[0]); err != nil {
return args, err
} else if err := ValidateType(*f, args[0]); err != nil {
return args, err
}
p.Namespace.Set(f.DestName, args[0])
return args[1:], nil
}
} else if strings.ContainsAny(f.ArgNum, "*+rR") {
if f.ArgNum == "+" && len(args) == 0 {
return args, TooFewArgsErr{*f}
}
var values []string
for len(args) > 0 {
if err := ValidateChoice(*f, args[0]); err != nil {
return args, err
} else if err := ValidateType(*f, args[0]); err != nil {
return args, err
}
values = append(values, args[0])
args = args[1:]
}
p.Namespace.Set(f.DestName, values)
return args, nil
} else if regexp.MustCompile(`^[1-9]+$`).MatchString(f.ArgNum) {
num, _ := strconv.Atoi(f.ArgNum)
if len(args) < num {
if f.IsRequired {
return args, TooFewArgsErr{*f}
}
return args, nil
}
if num > 1 {
var values []string
for _, v := range args[0:num] {
if err := ValidateChoice(*f, v); err != nil {
return args, err
} else if err := ValidateType(*f, v); err != nil {
return args, err
}
values = append(values, v)
}
p.Namespace.Set(f.DestName, values)
if num > len(args) {
args = []string{}
} else {
args = args[num:]
}
} else {
if err := ValidateChoice(*f, args[0]); err != nil {
return args, err
} else if err := ValidateType(*f, args[0]); err != nil {
return args, err
}
p.Namespace.Set(f.DestName, args[0])
if len(args) > 1 {
args = args[1:]
} else {
args = []string{}
}
}
}
return args, nil
}
// StoreConst stores the option's constant value into the parser. Provided
// arguments remain unmodified.
func StoreConst(p *Parser, f *Option, args ...string) ([]string, error) {
if f.ArgNum != "0" {
panic(fmt.Sprintf("option '%s' cannot expect any arguments.", f.DisplayName()))
}
p.Namespace.Set(f.DestName, f.ConstVal)
return args, nil
}
// StoreFalse stores a boolean `false` into the parser. Provided arguments remain
// unmodified.
func StoreFalse(p *Parser, f *Option, args ...string) ([]string, error) {
if f.ArgNum != "0" {
panic(fmt.Sprintf("option '%s' cannot expect any arguments.", f.DisplayName()))
}
p.Namespace.Set(f.DestName, "false")
return args, nil
}
// StoreTrue stores a boolean `true` into the parser. Provided arguments remain unmodified.
func StoreTrue(p *Parser, f *Option, args ...string) ([]string, error) {
if f.ArgNum != "0" {
panic(fmt.Sprintf("option '%s' cannot expect any arguments.", f.DisplayName()))
}
p.Namespace.Set(f.DestName, "true")
return args, nil
}
// Append retrives the appropriate number of argumnents for the current option, (if any),
// and appends them individually into the parser. Remaining arguments and errors are returned.
func Append(p *Parser, f *Option, args ...string) ([]string, error) {
appendValue := func(p *Parser, f *Option, value interface{}) error {
if !p.Namespace.KeyExists(f.DestName) {
p.Namespace.Set(f.DestName, make([]string, 0))
}
slice, err := p.Namespace.Try(f.DestName)
if err != nil {
return err
}
slice = append(slice.([]string), value.(string))
p.Namespace.Set(f.DestName, slice)
return nil
}
if regexp.MustCompile(`^[1-9]+$`).MatchString(f.ArgNum) {
num, _ := strconv.Atoi(f.ArgNum)
if len(args) < num {
return args, TooFewArgsErr{*f}
}
count := 0
for count < num {
if err := ValidateChoice(*f, args[0]); err != nil {
return args, err
} else if err := ValidateType(*f, args[0]); err != nil {
return args, err
}
appendValue(p, f, args[0])
args = args[1:]
count++
}
return args, nil
} else if f.ArgNum == "0" {
if isEnvVarFormat(f.DefaultVal) {
var (
defVal string
err error
)
if defVal, err = getEnvVar(f.DefaultVal); err != nil {
return args, err
}
appendValue(p, f, defVal)
} else {
appendValue(p, f, f.DefaultVal)
}
return args, nil
} else if f.ArgNum == "?" {
if len(args) > 0 {
if err := ValidateChoice(*f, args[0]); err != nil {
return args, err
} else if err := ValidateType(*f, args[0]); err != nil {
return args, err
}
appendValue(p, f, args[0])
args = args[1:]
} else {
if isEnvVarFormat(f.DefaultVal) {
var (
defVal string
err error
)
if defVal, err = getEnvVar(f.DefaultVal); err != nil {
return args, err
}
appendValue(p, f, defVal)
} else {
appendValue(p, f, f.DefaultVal)
}
}
} else if strings.ContainsAny(f.ArgNum, "*+rR") {
if f.ArgNum == "+" && len(args) == 0 {
return args, MissingOneOrMoreArgsErr{*f}
}
for len(args) > 0 {
if err := ValidateChoice(*f, args[0]); err != nil {
return args, err
} else if err := ValidateType(*f, args[0]); err != nil {
return args, err
}
appendValue(p, f, args[0])
args = args[1:]
}
return args, nil
}
return args, nil
}
// AppendConst appends the option's constant value into the parser. Provided arguments
// remain unmodified.
func AppendConst(p *Parser, f *Option, args ...string) ([]string, error) {
if f.ArgNum != "0" {
panic(fmt.Sprintf("option '%s' cannot expect any arguments.", f.DisplayName()))
}
if !p.Namespace.KeyExists(f.DestName) {
p.Namespace.Set(f.DestName, make([]string, 0))
}
slice, err := p.Namespace.Try(f.DestName)
if err != nil {
return nil, err
}
slice = append(slice.([]string), f.ConstVal)
p.Namespace.Set(f.DestName, slice)
return args, nil
}
// ShowHelp calls the parser's ShowHelp function to output parser usage information
// and help information for each option to stdout. Provided arguments remain unchanged.
// It returns a ShowHelpErr error instance, used to prevent further parsing.
func ShowHelp(p *Parser, f *Option, args ...string) ([]string, error) {
p.ShowHelp()
return args, ShowHelpErr{}
}
// ShowVersion calls the parser's ShowVersion function to output parser/program
// version information. Provided arguments remain unchanged. It returns a ShowVersionErr
// instance, used to prevent further parsing.
func ShowVersion(p *Parser, f *Option, args ...string) ([]string, error) {
p.ShowVersion()
return args, ShowVersionErr{}
}