forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uint8_slice.go
146 lines (123 loc) · 3.65 KB
/
uint8_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
145
146
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"fmt"
"strconv"
"strings"
)
// -- uint8Slice Value
type uint8SliceValue struct {
value *[]uint8
changed bool
}
var _ Value = (*uint8SliceValue)(nil)
var _ Getter = (*uint8SliceValue)(nil)
var _ SliceValue = (*uint8SliceValue)(nil)
var _ Typed = (*uint8SliceValue)(nil)
func newUint8SliceValue(val []uint8, p *[]uint8) *uint8SliceValue {
isv := new(uint8SliceValue)
isv.value = p
*isv.value = val
return isv
}
func (s *uint8SliceValue) Get() interface{} {
return *s.value
}
func (s *uint8SliceValue) Set(val string) error {
val = strings.TrimSpace(val)
temp64, err := strconv.ParseUint(val, 0, 8)
if err != nil {
return err
}
if !s.changed {
*s.value = []uint8{}
}
*s.value = append(*s.value, uint8(temp64))
s.changed = true
return nil
}
func (s *uint8SliceValue) Type() string {
return "uint8Slice"
}
func (s *uint8SliceValue) String() string {
if s.value == nil {
return "[]"
}
return fmt.Sprintf("%d", *s.value)
}
func (s *uint8SliceValue) fromString(val string) (uint8, error) {
t64, err := strconv.ParseUint(val, 0, 8)
if err != nil {
return 0, err
}
return uint8(t64), nil
}
func (s *uint8SliceValue) toString(val uint8) string {
return fmt.Sprintf("%d", val)
}
func (s *uint8SliceValue) Append(val string) error {
i, err := s.fromString(val)
if err != nil {
return err
}
*s.value = append(*s.value, i)
return nil
}
func (s *uint8SliceValue) Replace(val []string) error {
out := make([]uint8, len(val))
for i, d := range val {
var err error
out[i], err = s.fromString(d)
if err != nil {
return err
}
}
*s.value = out
return nil
}
func (s *uint8SliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
}
return out
}
// GetUint8Slice return the []uint8 value of a flag with the given name
func (fs *FlagSet) GetUint8Slice(name string) ([]uint8, error) {
val, err := fs.getFlagValue(name, "uint8Slice")
if err != nil {
return []uint8{}, err
}
return val.([]uint8), nil
}
// MustGetUint8Slice is like GetUint8Slice, but panics on error.
func (fs *FlagSet) MustGetUint8Slice(name string) []uint8 {
val, err := fs.GetUint8Slice(name)
if err != nil {
panic(err)
}
return val
}
// Uint8SliceVar defines a []uint8 flag with specified name, default value, and usage string.
// The argument p points to a []uint8 variable in which to store the value of the flag.
func (fs *FlagSet) Uint8SliceVar(p *[]uint8, name string, value []uint8, usage string, opts ...Opt) {
fs.Var(newUint8SliceValue(value, p), name, usage, opts...)
}
// Uint8SliceVar defines a []uint8 flag with specified name, default value, and usage string.
// The argument p points to a []uint8 variable in which to store the value of the flag.
func Uint8SliceVar(p *[]uint8, name string, value []uint8, usage string, opts ...Opt) {
CommandLine.Uint8SliceVar(p, name, value, usage, opts...)
}
// Uint8Slice defines a []uint8 flag with specified name, default value, and usage string.
// The return value is the address of a []uint8 variable that stores the value of the flag.
func (fs *FlagSet) Uint8Slice(name string, value []uint8, usage string, opts ...Opt) *[]uint8 {
var p []uint8
fs.Uint8SliceVar(&p, name, value, usage, opts...)
return &p
}
// Uint8Slice defines a []uint8 flag with specified name, default value, and usage string.
// The return value is the address of a []uint8 variable that stores the value of the flag.
func Uint8Slice(name string, value []uint8, usage string, opts ...Opt) *[]uint8 {
return CommandLine.Uint8Slice(name, value, usage, opts...)
}