github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/helper/flags/flag.go (about) 1 package flags 2 3 import ( 4 "strconv" 5 "strings" 6 "time" 7 ) 8 9 // StringFlag implements the flag.Value interface and allows multiple 10 // calls to the same variable to append a list. 11 type StringFlag []string 12 13 func (s *StringFlag) String() string { 14 return strings.Join(*s, ",") 15 } 16 17 func (s *StringFlag) Set(value string) error { 18 *s = append(*s, value) 19 return nil 20 } 21 22 // FuncVar is a type of flag that accepts a function that is the string 23 // given 24 // by the user. 25 type FuncVar func(s string) error 26 27 func (f FuncVar) Set(s string) error { return f(s) } 28 func (f FuncVar) String() string { return "" } 29 func (f FuncVar) IsBoolFlag() bool { return false } 30 31 // FuncBoolVar is a type of flag that accepts a function, converts the 32 // user's 33 // value to a bool, and then calls the given function. 34 type FuncBoolVar func(b bool) error 35 36 func (f FuncBoolVar) Set(s string) error { 37 v, err := strconv.ParseBool(s) 38 if err != nil { 39 return err 40 } 41 return f(v) 42 } 43 func (f FuncBoolVar) String() string { return "" } 44 func (f FuncBoolVar) IsBoolFlag() bool { return true } 45 46 // FuncDurationVar is a type of flag that 47 // accepts a function, converts the 48 // user's value to a duration, and then 49 // calls the given function. 50 type FuncDurationVar func(d time.Duration) error 51 52 func (f FuncDurationVar) Set(s string) error { 53 v, err := time.ParseDuration(s) 54 if err != nil { 55 return err 56 } 57 return f(v) 58 } 59 func (f FuncDurationVar) String() string { return "" } 60 func (f FuncDurationVar) IsBoolFlag() bool { return false } 61 62 // FuncOptionalStringVar is a flag that accepts a function which it 63 // calls on the optional string given by the user. 64 type FuncOptionalStringVar func(s string) error 65 66 func (f FuncOptionalStringVar) Set(s string) error { return f(s) } 67 func (f FuncOptionalStringVar) String() string { return "" } 68 func (f FuncOptionalStringVar) IsBoolFlag() bool { return true }