Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/pflag/int64_slice.go
2875 views
1
package pflag
2
3
import (
4
"fmt"
5
"strconv"
6
"strings"
7
)
8
9
// -- int64Slice Value
10
type int64SliceValue struct {
11
value *[]int64
12
changed bool
13
}
14
15
func newInt64SliceValue(val []int64, p *[]int64) *int64SliceValue {
16
isv := new(int64SliceValue)
17
isv.value = p
18
*isv.value = val
19
return isv
20
}
21
22
func (s *int64SliceValue) Set(val string) error {
23
ss := strings.Split(val, ",")
24
out := make([]int64, len(ss))
25
for i, d := range ss {
26
var err error
27
out[i], err = strconv.ParseInt(d, 0, 64)
28
if err != nil {
29
return err
30
}
31
32
}
33
if !s.changed {
34
*s.value = out
35
} else {
36
*s.value = append(*s.value, out...)
37
}
38
s.changed = true
39
return nil
40
}
41
42
func (s *int64SliceValue) Type() string {
43
return "int64Slice"
44
}
45
46
func (s *int64SliceValue) String() string {
47
out := make([]string, len(*s.value))
48
for i, d := range *s.value {
49
out[i] = fmt.Sprintf("%d", d)
50
}
51
return "[" + strings.Join(out, ",") + "]"
52
}
53
54
func (s *int64SliceValue) fromString(val string) (int64, error) {
55
return strconv.ParseInt(val, 0, 64)
56
}
57
58
func (s *int64SliceValue) toString(val int64) string {
59
return fmt.Sprintf("%d", val)
60
}
61
62
func (s *int64SliceValue) Append(val string) error {
63
i, err := s.fromString(val)
64
if err != nil {
65
return err
66
}
67
*s.value = append(*s.value, i)
68
return nil
69
}
70
71
func (s *int64SliceValue) Replace(val []string) error {
72
out := make([]int64, len(val))
73
for i, d := range val {
74
var err error
75
out[i], err = s.fromString(d)
76
if err != nil {
77
return err
78
}
79
}
80
*s.value = out
81
return nil
82
}
83
84
func (s *int64SliceValue) GetSlice() []string {
85
out := make([]string, len(*s.value))
86
for i, d := range *s.value {
87
out[i] = s.toString(d)
88
}
89
return out
90
}
91
92
func int64SliceConv(val string) (interface{}, error) {
93
val = strings.Trim(val, "[]")
94
// Empty string would cause a slice with one (empty) entry
95
if len(val) == 0 {
96
return []int64{}, nil
97
}
98
ss := strings.Split(val, ",")
99
out := make([]int64, len(ss))
100
for i, d := range ss {
101
var err error
102
out[i], err = strconv.ParseInt(d, 0, 64)
103
if err != nil {
104
return nil, err
105
}
106
107
}
108
return out, nil
109
}
110
111
// GetInt64Slice return the []int64 value of a flag with the given name
112
func (f *FlagSet) GetInt64Slice(name string) ([]int64, error) {
113
val, err := f.getFlagType(name, "int64Slice", int64SliceConv)
114
if err != nil {
115
return []int64{}, err
116
}
117
return val.([]int64), nil
118
}
119
120
// Int64SliceVar defines a int64Slice flag with specified name, default value, and usage string.
121
// The argument p points to a []int64 variable in which to store the value of the flag.
122
func (f *FlagSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string) {
123
f.VarP(newInt64SliceValue(value, p), name, "", usage)
124
}
125
126
// Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash.
127
func (f *FlagSet) Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) {
128
f.VarP(newInt64SliceValue(value, p), name, shorthand, usage)
129
}
130
131
// Int64SliceVar defines a int64[] flag with specified name, default value, and usage string.
132
// The argument p points to a int64[] variable in which to store the value of the flag.
133
func Int64SliceVar(p *[]int64, name string, value []int64, usage string) {
134
CommandLine.VarP(newInt64SliceValue(value, p), name, "", usage)
135
}
136
137
// Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash.
138
func Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) {
139
CommandLine.VarP(newInt64SliceValue(value, p), name, shorthand, usage)
140
}
141
142
// Int64Slice defines a []int64 flag with specified name, default value, and usage string.
143
// The return value is the address of a []int64 variable that stores the value of the flag.
144
func (f *FlagSet) Int64Slice(name string, value []int64, usage string) *[]int64 {
145
p := []int64{}
146
f.Int64SliceVarP(&p, name, "", value, usage)
147
return &p
148
}
149
150
// Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash.
151
func (f *FlagSet) Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 {
152
p := []int64{}
153
f.Int64SliceVarP(&p, name, shorthand, value, usage)
154
return &p
155
}
156
157
// Int64Slice defines a []int64 flag with specified name, default value, and usage string.
158
// The return value is the address of a []int64 variable that stores the value of the flag.
159
func Int64Slice(name string, value []int64, usage string) *[]int64 {
160
return CommandLine.Int64SliceP(name, "", value, usage)
161
}
162
163
// Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash.
164
func Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 {
165
return CommandLine.Int64SliceP(name, shorthand, value, usage)
166
}
167
168