-
Notifications
You must be signed in to change notification settings - Fork 1
/
args.go
53 lines (46 loc) · 1.1 KB
/
args.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package utils
import (
"strconv"
"github.com/feixiao/codis/pkg/utils/log"
)
func Argument(d map[string]interface{}, name string) (string, bool) {
if d[name] != nil {
if s, ok := d[name].(string); ok {
if s != "" {
return s, true
}
log.Panicf("option %s requires an argument", name)
} else {
log.Panicf("option %s isn't a valid string", name)
}
}
return "", false
}
func ArgumentMust(d map[string]interface{}, name string) string {
s, ok := Argument(d, name)
if ok {
return s
}
log.Panicf("option %s is required", name)
return ""
}
func ArgumentInteger(d map[string]interface{}, name string) (int, bool) {
if s, ok := Argument(d, name); ok {
n, err := strconv.Atoi(s)
if err != nil {
log.PanicErrorf(err, "option %s isn't a valid integer", name)
}
return n, true
}
return 0, false
}
func ArgumentIntegerMust(d map[string]interface{}, name string) int {
n, ok := ArgumentInteger(d, name)
if ok {
return n
}
log.Panicf("option %s is required", name)
return 0
}