1
1
package pflag
2
2
3
3
import (
4
+ "encoding/base64"
4
5
"encoding/hex"
5
6
"fmt"
6
7
"strings"
@@ -9,10 +10,12 @@ import (
9
10
// BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded
10
11
type bytesHexValue []byte
11
12
13
+ // String implements pflag.Value.String.
12
14
func (bytesHex bytesHexValue ) String () string {
13
15
return fmt .Sprintf ("%X" , []byte (bytesHex ))
14
16
}
15
17
18
+ // Set implements pflag.Value.Set.
16
19
func (bytesHex * bytesHexValue ) Set (value string ) error {
17
20
bin , err := hex .DecodeString (strings .TrimSpace (value ))
18
21
@@ -25,6 +28,7 @@ func (bytesHex *bytesHexValue) Set(value string) error {
25
28
return nil
26
29
}
27
30
31
+ // Type implements pflag.Value.Type.
28
32
func (* bytesHexValue ) Type () string {
29
33
return "bytesHex"
30
34
}
@@ -103,3 +107,103 @@ func BytesHex(name string, value []byte, usage string) *[]byte {
103
107
func BytesHexP (name , shorthand string , value []byte , usage string ) * []byte {
104
108
return CommandLine .BytesHexP (name , shorthand , value , usage )
105
109
}
110
+
111
+ // BytesBase64 adapts []byte for use as a flag. Value of flag is Base64 encoded
112
+ type bytesBase64Value []byte
113
+
114
+ // String implements pflag.Value.String.
115
+ func (bytesBase64 bytesBase64Value ) String () string {
116
+ return base64 .StdEncoding .EncodeToString ([]byte (bytesBase64 ))
117
+ }
118
+
119
+ // Set implements pflag.Value.Set.
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
+ // Type implements pflag.Value.Type.
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
+ // GetBytesBase64 return the []byte value of a flag with the given name
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
+ // BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
164
+ // The argument p points to an []byte variable in which to store the value of the flag.
165
+ func (f * FlagSet ) BytesBase64Var (p * []byte , name string , value []byte , usage string ) {
166
+ f .VarP (newBytesBase64Value (value , p ), name , "" , usage )
167
+ }
168
+
169
+ // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
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
+ // BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
175
+ // The argument p points to an []byte variable in which to store the value of the flag.
176
+ func BytesBase64Var (p * []byte , name string , value []byte , usage string ) {
177
+ CommandLine .VarP (newBytesBase64Value (value , p ), name , "" , usage )
178
+ }
179
+
180
+ // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
181
+ func BytesBase64VarP (p * []byte , name , shorthand string , value []byte , usage string ) {
182
+ CommandLine .VarP (newBytesBase64Value (value , p ), name , shorthand , usage )
183
+ }
184
+
185
+ // BytesBase64 defines an []byte flag with specified name, default value, and usage string.
186
+ // The return value is the address of an []byte variable that stores the value of the flag.
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
+ // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
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
+ // BytesBase64 defines an []byte flag with specified name, default value, and usage string.
201
+ // The return value is the address of an []byte variable that stores the value of the flag.
202
+ func BytesBase64 (name string , value []byte , usage string ) * []byte {
203
+ return CommandLine .BytesBase64P (name , "" , value , usage )
204
+ }
205
+
206
+ // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
207
+ func BytesBase64P (name , shorthand string , value []byte , usage string ) * []byte {
208
+ return CommandLine .BytesBase64P (name , shorthand , value , usage )
209
+ }
0 commit comments