code.cestus.io/tools/fabricator@v0.4.3/pkg/ff/ffpflag/pflag.go (about) 1 package ffpflag 2 3 import ( 4 "flag" 5 6 "github.com/spf13/pflag" 7 ) 8 9 // FlagSet is an adapter that makes a pflag.FlagSet usable as a ff.FlagSet. 10 // Flags declared using the P-suffixed variations of the pflag declaration 11 // functions, with both short and long names, must be referred to using their 12 // long names only in config files and environment variables. 13 type FlagSet struct { 14 *pflag.FlagSet 15 } 16 17 // NewFlagSet adapts the pflag.FlagSet to a ff.FlagSet. 18 func NewFlagSet(fs *pflag.FlagSet) *FlagSet { 19 return &FlagSet{fs} 20 } 21 22 // Parse implements ff.FlagSet. It calls pflag.FlagSet.Parse directly. 23 func (fs *FlagSet) Parse(arguments []string) error { 24 return fs.FlagSet.Parse(arguments) 25 } 26 27 // Visit implements ff.FlagSet. The flag.Flag provided to the passed function is 28 // a temporary concrete type constructed from the pflag.Flag. 29 func (fs *FlagSet) Visit(fn func(*flag.Flag)) { 30 fs.FlagSet.Visit(func(pf *pflag.Flag) { 31 fn(pflag2std(pf)) 32 }) 33 } 34 35 // VisitAll implements ff.FlagSet. The flag.Flag provided to the passed function 36 // is a temporary concrete type constructed from the pflag.Flag. 37 func (fs *FlagSet) VisitAll(fn func(*flag.Flag)) { 38 fs.FlagSet.VisitAll(func(pf *pflag.Flag) { 39 fn(pflag2std(pf)) 40 }) 41 } 42 43 // Set implements ff.FlagSet. It calls pflag.FlagSet.Set directly. 44 func (fs *FlagSet) Set(name, value string) error { 45 return fs.FlagSet.Set(name, value) 46 } 47 48 // Lookup implements ff.FlagSet. The returned flag.Flag is a temporary concrete 49 // type constructed from the pflag.Flag. 50 func (fs *FlagSet) Lookup(name string) *flag.Flag { 51 return pflag2std(fs.FlagSet.Lookup(name)) 52 } 53 54 func pflag2std(pFlag *pflag.Flag) *flag.Flag { 55 if pFlag == nil { 56 return nil 57 } 58 59 return &flag.Flag{ 60 Name: pFlag.Name, 61 Usage: pFlag.Usage, 62 Value: pFlag.Value, 63 DefValue: pFlag.DefValue, 64 } 65 }