1 package pflag
2
3 import (
4 "encoding/base64"
5 "encoding/hex"
6 "fmt"
7 "strings"
8 )
9
10
11 type bytesHexValue []byte
12
13
14 func (bytesHex bytesHexValue) String() string {
15 return fmt.Sprintf("%X", []byte(bytesHex))
16 }
17
18
19 func (bytesHex *bytesHexValue) Set(value string) error {
20 bin, err := hex.DecodeString(strings.TrimSpace(value))
21
22 if err != nil {
23 return err
24 }
25
26 *bytesHex = bin
27
28 return nil
29 }
30
31
32 func (*bytesHexValue) Type() string {
33 return "bytesHex"
34 }
35
36 func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue {
37 *p = val
38 return (*bytesHexValue)(p)
39 }
40
41 func bytesHexConv(sval string) (interface{}, error) {
42
43 bin, err := hex.DecodeString(sval)
44
45 if err == nil {
46 return bin, nil
47 }
48
49 return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
50 }
51
52
53 func (f *FlagSet) GetBytesHex(name string) ([]byte, error) {
54 val, err := f.getFlagType(name, "bytesHex", bytesHexConv)
55
56 if err != nil {
57 return []byte{}, err
58 }
59
60 return val.([]byte), nil
61 }
62
63
64
65 func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) {
66 f.VarP(newBytesHexValue(value, p), name, "", usage)
67 }
68
69
70 func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
71 f.VarP(newBytesHexValue(value, p), name, shorthand, usage)
72 }
73
74
75
76 func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
77 CommandLine.VarP(newBytesHexValue(value, p), name, "", usage)
78 }
79
80
81 func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
82 CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
83 }
84
85
86
87 func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte {
88 p := new([]byte)
89 f.BytesHexVarP(p, name, "", value, usage)
90 return p
91 }
92
93
94 func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
95 p := new([]byte)
96 f.BytesHexVarP(p, name, shorthand, value, usage)
97 return p
98 }
99
100
101
102 func BytesHex(name string, value []byte, usage string) *[]byte {
103 return CommandLine.BytesHexP(name, "", value, usage)
104 }
105
106
107 func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
108 return CommandLine.BytesHexP(name, shorthand, value, usage)
109 }
110
111
112 type bytesBase64Value []byte
113
114
115 func (bytesBase64 bytesBase64Value) String() string {
116 return base64.StdEncoding.EncodeToString([]byte(bytesBase64))
117 }
118
119
120 func (bytesBase64 *bytesBase64Value) Set(value string) error {
121 bin, err := base64.StdEncoding.DecodeString(strings.TrimSpace(value))
122
123 if err != nil {
124 return err
125 }
126
127 *bytesBase64 = bin
128
129 return nil
130 }
131
132
133 func (*bytesBase64Value) Type() string {
134 return "bytesBase64"
135 }
136
137 func newBytesBase64Value(val []byte, p *[]byte) *bytesBase64Value {
138 *p = val
139 return (*bytesBase64Value)(p)
140 }
141
142 func bytesBase64ValueConv(sval string) (interface{}, error) {
143
144 bin, err := base64.StdEncoding.DecodeString(sval)
145 if err == nil {
146 return bin, nil
147 }
148
149 return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
150 }
151
152
153 func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) {
154 val, err := f.getFlagType(name, "bytesBase64", bytesBase64ValueConv)
155
156 if err != nil {
157 return []byte{}, err
158 }
159
160 return val.([]byte), nil
161 }
162
163
164
165 func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
166 f.VarP(newBytesBase64Value(value, p), name, "", usage)
167 }
168
169
170 func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
171 f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
172 }
173
174
175
176 func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
177 CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage)
178 }
179
180
181 func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
182 CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
183 }
184
185
186
187 func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte {
188 p := new([]byte)
189 f.BytesBase64VarP(p, name, "", value, usage)
190 return p
191 }
192
193
194 func (f *FlagSet) BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {
195 p := new([]byte)
196 f.BytesBase64VarP(p, name, shorthand, value, usage)
197 return p
198 }
199
200
201
202 func BytesBase64(name string, value []byte, usage string) *[]byte {
203 return CommandLine.BytesBase64P(name, "", value, usage)
204 }
205
206
207 func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {
208 return CommandLine.BytesBase64P(name, shorthand, value, usage)
209 }
210
View as plain text