This repository was archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflag_slice.go
144 lines (116 loc) · 4.02 KB
/
flag_slice.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
137
138
139
140
141
142
143
144
package flag
import (
"encoding/json"
"strconv"
)
type boolSlice []bool
func newBoolSliceValue(val []bool, p *[]bool) *boolSlice {
*p = val
return (*boolSlice)(p)
}
func (b *boolSlice) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return err
}
*b = append(*b, v)
return nil
}
func (b *boolSlice) String() string {
all, err := json.Marshal(b)
if err != nil {
panic(err.Error())
}
return string(all)
}
func (b *boolSlice) Get() interface{} {
return []bool(*b)
}
type int64SliceValue []int64
func newInt64SliceValue(val []int64, p *[]int64) *int64SliceValue {
*p = val
return (*int64SliceValue)(p)
}
func (i *int64SliceValue) Set(val string) error {
var iv int64Value
err := iv.Set(val)
if err != nil {
return err
}
*i = append(*i, int64(iv))
return nil
}
func (i *int64SliceValue) Get() interface{} {
return []int64(*i)
}
func (i *int64SliceValue) String() string {
all, err := json.Marshal(i)
if err != nil {
panic(err.Error())
}
return string(all)
}
// Int64SliceVar defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 slice variable in which to store the value of the flag.
func (f *FlagSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string) {
f.Var(newInt64SliceValue(value, p), name, usage)
}
// Int64SliceVar defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 slice variable in which to store the value of the flag.
func Int64SliceVar(p *[]int64, name string, value []int64, usage string) {
CommandLine.Var(newInt64SliceValue(value, p), name, usage)
}
// Int64Slice defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 slice variable that stores the value of the flag.
func (f *FlagSet) Int64Slice(name string, value []int64, usage string) *[]int64 {
p := new([]int64)
f.Int64SliceVar(p, name, value, usage)
return p
}
// Int64Slice defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 slice variable that stores the value of the flag.
func Int64Slice(name string, value []int64, usage string) *[]int64 {
return CommandLine.Int64Slice(name, value, usage)
}
// -- string slice value
type stringSliceValue []string
func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
*p = val
return (*stringSliceValue)(p)
}
func (s *stringSliceValue) Set(val string) error {
*s = append(*s, val)
return nil
}
func (s *stringSliceValue) Get() interface{} {
return []string(*s)
}
func (s *stringSliceValue) String() string {
all, err := json.Marshal(s)
if err != nil {
panic(err.Error())
}
return string(all)
}
// StringSliceVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string slice variable in which to store the value of the flag.
func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
f.Var(newStringSliceValue(value, p), name, usage)
}
// StringSliceVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string slice variable in which to store the value of the flag.
func StringSliceVar(p *[]string, name string, value []string, usage string) {
CommandLine.Var(newStringSliceValue(value, p), name, usage)
}
// StringSlice defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string slice variable that stores the value of the flag.
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
p := new([]string)
f.StringSliceVar(p, name, value, usage)
return p
}
// StringSlice defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string slice variable that stores the value of the flag.
func StringSlice(name string, value []string, usage string) *[]string {
return CommandLine.StringSlice(name, value, usage)
}