-
Notifications
You must be signed in to change notification settings - Fork 47
/
flag.go
270 lines (244 loc) · 6.6 KB
/
flag.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"encoding/hex"
"errors"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/iov-one/weave"
"github.com/iov-one/weave/coin"
"github.com/iov-one/weave/x/gov"
)
// flAddress returns a value that is being initialized with given default value
// and optionally overwritten by a command line argument if provided. This
// function follows Go's flag package convention.
// If given value cannot be deserialized to required type, process is
// terminated.
func flAddress(fl *flag.FlagSet, name, defaultVal, usage string) *weave.Address {
var a weave.Address
if defaultVal != "" {
var err error
a, err = weave.ParseAddress(defaultVal)
if err != nil {
flagDie("Cannot parse %q weave.Address flag value. %s", name, err)
}
}
fl.Var(&a, name, usage)
return &a
}
// flCoin returns a value that is being initialized with given default value
// and optionally overwritten by a command line argument if provided. This
// function follows Go's flag package convention.
// If given value cannot be deserialized to required type, process is
// terminated.
func flCoin(fl *flag.FlagSet, name, defaultVal, usage string) *coin.Coin {
var c coin.Coin
if defaultVal != "" {
var err error
c, err = coin.ParseHumanFormat(defaultVal)
if err != nil {
flagDie("Cannot parse %q wave.Coin flag value. %s", name, err)
}
}
fl.Var(&c, name, usage)
return &c
}
func flTime(fl *flag.FlagSet, name string, defaultVal func() time.Time, usage string) *flagTime {
var t flagTime
if defaultVal != nil {
t = flagTime{time: defaultVal()}
}
fl.Var(&t, name, usage)
return &t
}
// flagTime is created to be used as a time.Time that implements flag.Value
// interface.
type flagTime struct {
time time.Time
}
func (t flagTime) String() string {
return t.time.Format(flagTimeFormat)
}
func (t *flagTime) Set(raw string) error {
val, err := time.Parse(flagTimeFormat, raw)
if err != nil {
return err
}
t.time = val
return nil
}
func (t *flagTime) Time() time.Time {
return t.time
}
func (t *flagTime) UnixTime() weave.UnixTime {
return weave.AsUnixTime(t.time)
}
const flagTimeFormat = "2006-01-02 15:04"
// flHex returns a value that is being initialized with given default value
// and optionally overwritten by a command line argument if provided. This
// function follows Go's flag package convention.
// If given value cannot be deserialized to required type, process is
// terminated.
func flHex(fl *flag.FlagSet, name, defaultVal, usage string) *flagbytes {
var b []byte
if defaultVal != "" {
var err error
b, err = hex.DecodeString(defaultVal)
if err != nil {
flagDie("Cannot parse %q hex encoded flag value. %s", name, err)
}
}
var fb flagbytes = b
fl.Var(&fb, name, usage)
return &fb
}
// flagbytes is created to be used as a byte array that implements flag.Value
// interface. It is using hex encoding to transform into a string
// representation.
type flagbytes []byte
func (b flagbytes) String() string {
return hex.EncodeToString(b)
}
func (b *flagbytes) Set(raw string) error {
val, err := hex.DecodeString(raw)
if err != nil {
return err
}
*b = val
return nil
}
// flagDie terminates the program when a flag parsing was not successful. This
// is a variable so that it can be overwritten for the tests.
var flagDie = func(description string, args ...interface{}) {
s := fmt.Sprintf(description, args...)
fmt.Fprintln(os.Stderr, s)
os.Exit(2)
}
// flSeq returns a value that is being initialized with given default value
// and optionally overwritten by a command line argument if provided. This
// function follows Go's flag package convention.
// If given value cannot be deserialized to required type, process is
// terminated.
// Sequence can be serialized using one of the following formats:
// - decimal number converted to string
// - hex serialized binary representation
// - base64 serialized binary representation
func flSeq(fl *flag.FlagSet, name, defaultVal, usage string) *flagseq {
var b []byte
if defaultVal != "" {
var err error
b, err = unpackSequence(defaultVal)
if err != nil {
flagDie("Cannot parse %q sequence flag value. %s", name, err)
}
}
var fs flagseq = b
fl.Var(&fs, name, usage)
return &fs
}
type flagseq []byte
func (b flagseq) String() string {
if len(b) == 0 {
return ""
}
n, err := fromSequence(b)
if err != nil {
panic(fmt.Sprintf("%q is not a valid sequence value: %s", []byte(b), err))
}
return fmt.Sprint(n)
}
func (b *flagseq) Set(raw string) error {
val, err := unpackSequence(raw)
if err != nil {
return err
}
*b = val
return nil
}
func flFraction(fl *flag.FlagSet, name, defaultVal, usage string) *flagfraction {
var ff flagfraction
if defaultVal != "" {
if f, err := unpackFraction(defaultVal); err != nil {
flagDie("Cannot parse %q fraction flag value. %s", name, err)
} else {
ff.frac = &gov.Fraction{
Numerator: f.Numerator,
Denominator: f.Denominator,
}
}
}
fl.Var(&ff, name, usage)
return &ff
}
type flagfraction struct {
frac *gov.Fraction
}
func unpackFraction(s string) (*gov.Fraction, error) {
chunks := strings.Split(s, "/")
switch len(chunks) {
case 1:
n, err := strconv.Atoi(chunks[0])
if err != nil {
return nil, fmt.Errorf("cannot parse numerator value %q: %s", chunks[0], err)
}
if n < 0 {
return nil, fmt.Errorf("numerator value cannot be negative: %d", n)
}
if n == 0 {
return &gov.Fraction{Numerator: 0, Denominator: 0}, nil
}
return &gov.Fraction{Numerator: uint32(n), Denominator: 1}, nil
case 2:
n, err := strconv.Atoi(chunks[0])
if err != nil {
return nil, fmt.Errorf("cannot parse numerator value %q: %s", chunks[0], err)
}
if n < 0 {
return nil, fmt.Errorf("numerator value cannot be negative: %d", n)
}
d, err := strconv.Atoi(chunks[1])
if err != nil {
return nil, fmt.Errorf("cannot parse denumerator value %q: %s", chunks[0], err)
}
if d < 0 {
return nil, fmt.Errorf("denumerator value cannot be negative: %d", n)
}
if d == 0 {
return nil, errors.New("denumerator must not be zero")
}
return &gov.Fraction{Numerator: uint32(n), Denominator: uint32(d)}, nil
default:
return nil, errors.New("invalid fraction format")
}
}
func (f flagfraction) String() string {
if f.frac == nil {
return ""
}
if f.frac.Numerator == 0 {
return "0"
}
if f.frac.Denominator == 1 {
return fmt.Sprint(f.frac.Numerator)
}
return fmt.Sprintf("%d/%d", f.frac.Numerator, f.frac.Denominator)
}
func (f *flagfraction) Set(raw string) error {
val, err := unpackFraction(raw)
if err != nil {
return err
}
f.frac = val
return nil
}
func (f *flagfraction) Fraction() *gov.Fraction {
if f.frac == nil {
return nil
}
// copy
frac := *f.frac
return &frac
}