-
Notifications
You must be signed in to change notification settings - Fork 71
/
flags.go
59 lines (49 loc) · 1.61 KB
/
flags.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"strconv"
"time"
)
// funcVar is a type of flag that accepts a function that is the string given
// by the user.
type funcVar func(s string) error
func (f funcVar) Set(s string) error { return f(s) }
func (f funcVar) String() string { return "" }
func (f funcVar) IsBoolFlag() bool { return false }
// funcBoolVar is a type of flag that accepts a function, converts the user's
// value to a bool, and then calls the given function.
type funcBoolVar func(b bool) error
func (f funcBoolVar) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return err
}
return f(v)
}
func (f funcBoolVar) String() string { return "" }
func (f funcBoolVar) IsBoolFlag() bool { return true }
// funcDurationVar is a type of flag that accepts a function, converts the
// user's value to a duration, and then calls the given function.
type funcDurationVar func(d time.Duration) error
func (f funcDurationVar) Set(s string) error {
v, err := time.ParseDuration(s)
if err != nil {
return err
}
return f(v)
}
func (f funcDurationVar) String() string { return "" }
func (f funcDurationVar) IsBoolFlag() bool { return false }
// funcIntVar is a type of flag that accepts a function, converts the
// user's value to a int, and then calls the given function.
type funcIntVar func(i int) error
func (f funcIntVar) Set(s string) error {
v, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return err
}
return f(int(v))
}
func (f funcIntVar) String() string { return "" }
func (f funcIntVar) IsBoolFlag() bool { return false }