-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflagset.go
92 lines (80 loc) Β· 5.59 KB
/
flagset.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
package scotty
import (
"flag"
"os"
"strconv"
"time"
)
// FlagSet wraps flag.FlagSet and adds a few methods like
// StringVarE, BoolVarE and similar methods for other types.
type FlagSet struct{ *flag.FlagSet }
// StringVarE defines a string flag and environment variable with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) StringVarE(p *string, flagName, envName, value, usage string) {
f.FlagSet.StringVar(p, flagName, tern[string](os.Getenv(envName) != "", os.Getenv(envName), value), usage)
}
// BoolVarE defines a bool flag and environment variable with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) BoolVarE(p *bool, flagName, envName string, value bool, usage string) {
parsed, err := strconv.ParseBool(os.Getenv(envName))
f.FlagSet.BoolVar(p, flagName, tern[bool](err == nil, parsed, value), usage)
}
// IntVarE defines an int flag and environment variable with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) IntVarE(p *int, flagName, envName string, value int, usage string) {
parsed, err := strconv.Atoi(os.Getenv(envName))
f.FlagSet.IntVar(p, flagName, tern[int](err == nil, parsed, value), usage)
}
// Int64VarE defines an int64 flag and environment variable with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) Int64VarE(p *int64, flagName, envName string, value int64, usage string) {
parsed, err := strconv.Atoi(os.Getenv(envName))
f.FlagSet.Int64Var(p, flagName, tern[int64](err == nil, int64(parsed), value), usage)
}
// Float64VarE defines a float64 flag and environment variable with specified name, default value, and usage string.
// The argument p points to a float64 variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) Float64VarE(p *float64, flagName, envName string, value float64, usage string) {
parsed, err := strconv.ParseFloat(os.Getenv(envName), 64)
f.FlagSet.Float64Var(p, flagName, tern[float64](err == nil, parsed, value), usage)
}
// UintVarE defines an uint flag and environment variable with specified name, default value, and usage string.
// The argument p points to an uint variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) UintVarE(p *uint, flagName, envName string, value uint, usage string) {
parsed, err := strconv.Atoi(os.Getenv(envName))
f.FlagSet.UintVar(p, flagName, tern[uint](err == nil, uint(parsed), value), usage)
}
// Uint64VarE defines an uint64 flag and environment variable with specified name, default value, and usage string.
// The argument p points to an uint64 variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) Uint64VarE(p *uint64, flagName, envName string, value uint64, usage string) {
parsed, err := strconv.Atoi(os.Getenv(envName))
f.FlagSet.Uint64Var(p, flagName, tern[uint64](err == nil, uint64(parsed), value), usage)
}
// DurationVarE defines a time.Duration flag and environment variable with specified name, default value, and usage string.
// The argument p points to a time.Duration variable in which to store the value of the flag or environment variable.
// Flag has priority over environment variable. If flag not set the environment variable value will be used.
// If the value of environment variable can't be parsed to destination type the default value will be used.
func (f *FlagSet) DurationVarE(p *time.Duration, flagName, envName string, value time.Duration, usage string) {
parsed, err := time.ParseDuration(os.Getenv(envName))
f.FlagSet.DurationVar(p, flagName, tern[time.Duration](err == nil, parsed, value), usage)
}
//nolint:revive // flag-parameter is ok here.
func tern[T any](cond bool, t, f T) T {
if cond {
return t
}
return f
}