github.com/puellanivis/breton@v0.2.16/lib/gnuflag/option.go (about) 1 package gnuflag 2 3 import ( 4 "fmt" 5 ) 6 7 // Option is a function that sets a specified function upon a flag.Flag. 8 // It returns an Option that will revert the option set. 9 type Option func(*Flag) (Option, error) 10 11 // WithShort returns an Option that will set the Short Flag of a flag to the 12 // specified rune. 13 func WithShort(shortFlag rune) Option { 14 return func(f *Flag) (Option, error) { 15 save := f.Short 16 17 f.Short = shortFlag 18 19 return WithShort(save), nil 20 } 21 } 22 23 // WithDefault returns an Option that will set the default value of a flag 24 // to the value given. If when setting the value, the values cannot be assigned 25 // to the flag, then it will panic. (This should mostly be during initilization 26 // so while it cannot be checked at compile-time, it should result in an 27 // immediate panic when running the program.) 28 func WithDefault(value interface{}) Option { 29 return func(f *Flag) (Option, error) { 30 save := f.Value.Get() 31 if err := f.Value.Set(fmt.Sprint(value)); err != nil { 32 return WithDefault(save), err 33 } 34 35 f.DefValue = f.Value.String() 36 37 return WithDefault(save), nil 38 } 39 }