-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocopt.go
134 lines (114 loc) · 2.56 KB
/
docopt.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
package commander
import (
"fmt"
"strconv"
)
// DocoptMap docopt returns a map of option names to the values
type DocoptMap map[string]interface{}
func newDocoptMap(m map[string]interface{}) DocoptMap {
return DocoptMap(m)
}
func (d DocoptMap) Map() map[string]interface{} {
return map[string]interface{}(d)
}
func (d DocoptMap) Get(key string) interface{} {
if v, ok := d[key]; ok {
return v
}
return nil
}
func (d DocoptMap) Contain(key string) bool {
if v, ok := d[key]; ok && v != nil {
switch i := v.(type) {
case string:
return len(i) != 0 &&
i != "0" && i != "false"
case []string:
return len(i) != 0
case bool:
return i
case int, int8, int16, int32, int64:
return i != 0
case float32, float64:
return i != 0
}
}
return false
}
func (d DocoptMap) GetString(key string) (string, bool) {
if v := d.Get(key); v != nil {
if s, ok := v.(string); ok {
return s, true
}
return fmt.Sprintf("%v", v), true
}
return "", false
}
func (d DocoptMap) MustString(key string) string {
if s, ok := d.GetString(key); ok {
return s
}
return ""
}
func (d DocoptMap) GetStrings(key string) ([]string, bool) {
if v := d.Get(key); v != nil {
if s, ok := v.([]string); ok {
return s, true
}
}
return nil, false
}
func (d DocoptMap) MustStrings(key string) []string {
if s, ok := d.GetStrings(key); ok {
return s
}
return []string{}
}
func (d DocoptMap) GetBool(key string) (bool, bool) {
b, err := strconv.ParseBool(d.MustString(key))
return b, err == nil
}
func (d DocoptMap) MustBool(key string) bool {
b, ok := d.GetBool(key)
return b && ok
}
func (d DocoptMap) GetInt64(key string) (int64, bool) {
i, err := strconv.ParseInt(d.MustString(key), 10, 64)
return i, err == nil
}
func (d DocoptMap) MustInt64(key string) int64 {
if i, ok := d.GetInt64(key); ok {
return i
}
return 0
}
func (d DocoptMap) GetInt(key string) (int, bool) {
i, err := strconv.ParseInt(d.MustString(key), 10, 32)
return int(i), err == nil
}
func (d DocoptMap) MustInt(key string) int {
if i, ok := d.GetInt(key); ok {
return i
}
return 0
}
func (d DocoptMap) GetFloat64(key string) (float64, bool) {
f, err := strconv.ParseFloat(d.MustString(key), 64)
return f, err == nil
}
func (d DocoptMap) MustFloat64(key string) float64 {
if f, ok := d.GetFloat64(key); ok {
return f
}
return 0
}
func (d DocoptMap) GetFloat(key string) (float32, bool) {
f, err := strconv.ParseFloat(d.MustString(key), 32)
return float32(f), err == nil
}
func (d DocoptMap) MustFloat(key string) float32 {
if f, ok := d.GetFloat(key); ok {
return f
}
return 0
}