-
Notifications
You must be signed in to change notification settings - Fork 10
/
typed.go
57 lines (47 loc) · 1.16 KB
/
typed.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
package parser
import (
"fmt"
"strconv"
)
type argumentsParser struct {
arguments map[string]string
usedArguments map[string]struct{}
err error
}
func newArgumentsParser(arguments map[string]string) *argumentsParser {
return &argumentsParser{
arguments: arguments,
usedArguments: make(map[string]struct{}),
err: nil,
}
}
// GetInt tries to parse the argument name and returns its integer value or defaultValue if the argument is not found.
// The return value is valid only if Err returns nil.
func (ap *argumentsParser) GetInt(name string, defaultValue int) int {
if ap.err != nil {
return -1
}
raw, found := ap.arguments[name]
if !found {
return defaultValue
}
val, err := strconv.Atoi(raw)
if err != nil {
ap.err = fmt.Errorf("%q needs an integer value", name)
return -1
}
ap.usedArguments[name] = struct{}{}
return val
}
// Err returns the first error encountered by the ArgumentsParser
func (ap *argumentsParser) Err() error {
return ap.err
}
func (ap *argumentsParser) firstUnusedArgument() string {
for arg := range ap.arguments {
if _, ok := ap.usedArguments[arg]; !ok {
return arg
}
}
return ""
}