/
utils.go
62 lines (56 loc) · 1.5 KB
/
utils.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
package main
type ArgumentList []string
func (args ArgumentList) HasArgument(arg string) bool {
for _, argCandidate := range args {
if arg == argCandidate {
return true
}
}
return false
}
func (args ArgumentList) GetArgument(argname, defaultVal string) string {
for idx, argCandidate := range args {
if argname == argCandidate {
// If it's the last argument, then return string
// "true" because we can't return idx+1, but it
// shouldn't be the default value when the argument
// isn't provided either..
if idx >= len(args)-1 {
return "true"
}
return args[idx+1]
}
}
return defaultVal
}
func (args ArgumentList) GetAndRemoveArguments(argnames []string) (ArgumentList, []string) {
var nextArgumentType int = -1
matches := make([]string, len(argnames))
var retArgs []string
for idx, argCandidate := range args {
// The last token was in argnames, so this one
// is the value. Set it in matches and reset
// nextArgumentType and continue to the next
// possible token
if nextArgumentType != -1 {
matches[nextArgumentType] = argCandidate
nextArgumentType = -1
continue
}
// Check if this is a argname we're looking for
for argidx, argname := range argnames {
if argname == argCandidate {
if idx >= len(args)-1 {
matches[argidx] = "true"
}
nextArgumentType = argidx
break
}
}
// It wasn't an argname, so add it to the return
if nextArgumentType == -1 {
retArgs = append(retArgs, argCandidate)
}
}
return retArgs, matches
}