github.com/endophage/viper@v0.0.0-20160111150723-a212099cbe6f/flags.go (about)

     1  package viper
     2  
     3  import "github.com/spf13/pflag"
     4  
     5  // FlagValueSet is an interface that users can implement
     6  // to bind a set of flags to viper.
     7  type FlagValueSet interface {
     8  	VisitAll(fn func(FlagValue))
     9  }
    10  
    11  // FlagValue is an interface that users can implement
    12  // to bind different flags to viper.
    13  type FlagValue interface {
    14  	HasChanged() bool
    15  	Name() string
    16  	ValueString() string
    17  	ValueType() string
    18  }
    19  
    20  // pflagValueSet is a wrapper around *pflag.ValueSet
    21  // that implements FlagValueSet.
    22  type pflagValueSet struct {
    23  	flags *pflag.FlagSet
    24  }
    25  
    26  // VisitAll iterates over all *pflag.Flag inside the *pflag.FlagSet.
    27  func (p pflagValueSet) VisitAll(fn func(flag FlagValue)) {
    28  	p.flags.VisitAll(func(flag *pflag.Flag) {
    29  		fn(pflagValue{flag})
    30  	})
    31  }
    32  
    33  // pflagValue is a wrapper aroung *pflag.flag
    34  // that implements FlagValue
    35  type pflagValue struct {
    36  	flag *pflag.Flag
    37  }
    38  
    39  // HasChanges returns whether the flag has changes or not.
    40  func (p pflagValue) HasChanged() bool {
    41  	return p.flag.Changed
    42  }
    43  
    44  // Name returns the name of the flag.
    45  func (p pflagValue) Name() string {
    46  	return p.flag.Name
    47  }
    48  
    49  // ValueString returns the value of the flag as a string.
    50  func (p pflagValue) ValueString() string {
    51  	return p.flag.Value.String()
    52  }
    53  
    54  // ValueType returns the type of the flag as a string.
    55  func (p pflagValue) ValueType() string {
    56  	return p.flag.Value.Type()
    57  }