OSDN Git Service

equity compiler tool (#18)
[bytom/equity.git] / vendor / github.com / spf13 / pflag / bytes.go
1 package pflag
2
3 import (
4         "encoding/base64"
5         "encoding/hex"
6         "fmt"
7         "strings"
8 )
9
10 // BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded
11 type bytesHexValue []byte
12
13 // String implements pflag.Value.String.
14 func (bytesHex bytesHexValue) String() string {
15         return fmt.Sprintf("%X", []byte(bytesHex))
16 }
17
18 // Set implements pflag.Value.Set.
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 // Type implements pflag.Value.Type.
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 // GetBytesHex return the []byte value of a flag with the given name
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 // BytesHexVar defines an []byte flag with specified name, default value, and usage string.
64 // The argument p points to an []byte variable in which to store the value of the flag.
65 func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) {
66         f.VarP(newBytesHexValue(value, p), name, "", usage)
67 }
68
69 // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
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 // BytesHexVar defines an []byte flag with specified name, default value, and usage string.
75 // The argument p points to an []byte variable in which to store the value of the flag.
76 func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
77         CommandLine.VarP(newBytesHexValue(value, p), name, "", usage)
78 }
79
80 // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
81 func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
82         CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
83 }
84
85 // BytesHex defines an []byte flag with specified name, default value, and usage string.
86 // The return value is the address of an []byte variable that stores the value of the flag.
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 // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
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 // BytesHex defines an []byte flag with specified name, default value, and usage string.
101 // The return value is the address of an []byte variable that stores the value of the flag.
102 func BytesHex(name string, value []byte, usage string) *[]byte {
103         return CommandLine.BytesHexP(name, "", value, usage)
104 }
105
106 // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
107 func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
108         return CommandLine.BytesHexP(name, shorthand, value, usage)
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 }