-
Notifications
You must be signed in to change notification settings - Fork 33
/
options.go
136 lines (118 loc) · 3.39 KB
/
options.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
package goptions
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
)
type optionFunc func(f *Flag, option, value string) error
type optionMap map[string]optionFunc
var (
typeOptionMap = map[reflect.Type]optionMap{
// Global options
nil: optionMap{
"description": description,
"obligatory": obligatory,
"mutexgroup": mutexgroup,
},
reflect.TypeOf(new(time.Time)).Elem(): optionMap{
"format": time_format,
},
reflect.TypeOf(new(*os.File)).Elem(): optionMap{
"create": initOptionMeta(file_create, "file_mode", 0),
"append": initOptionMeta(file_append, "file_mode", 0),
"rdonly": initOptionMeta(file_rdonly, "file_mode", 0),
"wronly": initOptionMeta(file_wronly, "file_mode", 0),
"rdwr": initOptionMeta(file_rdwr, "file_mode", 0),
"excl": initOptionMeta(file_excl, "file_mode", 0),
"sync": initOptionMeta(file_sync, "file_mode", 0),
"trunc": initOptionMeta(file_trunc, "file_mode", 0),
"perm": file_perm,
},
}
)
// Wraps another optionFunc and inits optionMeta[field] with value if it does
// not have one already.
func initOptionMeta(fn optionFunc, field string, init_value interface{}) optionFunc {
return func(f *Flag, option, value string) error {
if _, ok := f.optionMeta[field]; !ok {
f.optionMeta[field] = init_value
}
return fn(f, option, value)
}
}
func description(f *Flag, option, value string) error {
f.Description = strings.Replace(value, `\`, ``, -1)
return nil
}
func obligatory(f *Flag, option, value string) error {
f.Obligatory = true
return nil
}
func mutexgroup(f *Flag, option, value string) error {
if len(value) <= 0 {
return fmt.Errorf("Mutexgroup option needs a value")
}
for _, group := range strings.Split(value, ",") {
f.MutexGroups = append(f.MutexGroups, group)
}
return nil
}
func file_create(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_CREATE
return nil
}
func file_append(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_APPEND
return nil
}
func file_rdonly(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_RDONLY
return nil
}
func file_wronly(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_WRONLY
return nil
}
func file_rdwr(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_RDWR
return nil
}
func file_excl(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_EXCL
return nil
}
func file_sync(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_SYNC
return nil
}
func file_trunc(f *Flag, option, value string) error {
f.optionMeta["file_mode"] = f.optionMeta["file_mode"].(int) | os.O_TRUNC
return nil
}
func file_perm(f *Flag, option, value string) error {
perm, err := strconv.ParseInt(value, 8, 32)
if err != nil {
return err
}
f.optionMeta["file_perm"] = uint32(perm)
return nil
}
func time_format(f *Flag, option, value string) error {
f.optionMeta["format"] = value
return nil
}
func optionMapForType(t reflect.Type) optionMap {
g := typeOptionMap[nil]
m, _ := typeOptionMap[t]
r := make(optionMap)
for k, v := range g {
r[k] = v
}
for k, v := range m {
r[k] = v
}
return r
}