cosmossdk.io/client/v2@v2.0.0-beta.1/autocli/flag/simple.go (about)

     1  package flag
     2  
     3  import (
     4  	"github.com/spf13/pflag"
     5  	"google.golang.org/protobuf/reflect/protoreflect"
     6  )
     7  
     8  func bindSimpleFlag(flagSet *pflag.FlagSet, kind protoreflect.Kind, name, shorthand, usage string) HasValue {
     9  	switch kind {
    10  	case protoreflect.StringKind:
    11  		val := flagSet.StringP(name, shorthand, "", usage)
    12  		return newSimpleValue(val, protoreflect.ValueOfString)
    13  	case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
    14  		val := flagSet.Uint32P(name, shorthand, 0, usage)
    15  		return newSimpleValue(val, protoreflect.ValueOfUint32)
    16  	case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
    17  		val := flagSet.Uint64P(name, shorthand, 0, usage)
    18  		return newSimpleValue(val, protoreflect.ValueOfUint64)
    19  	case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
    20  		val := flagSet.Int32P(name, shorthand, 0, usage)
    21  		return newSimpleValue(val, protoreflect.ValueOfInt32)
    22  	case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
    23  		val := flagSet.Int64P(name, shorthand, 0, usage)
    24  		return newSimpleValue(val, protoreflect.ValueOfInt64)
    25  	case protoreflect.BoolKind:
    26  		val := flagSet.BoolP(name, shorthand, false, usage)
    27  		return newSimpleValue(val, protoreflect.ValueOfBool)
    28  	default:
    29  		return nil
    30  	}
    31  }
    32  
    33  type simpleValue[T any] struct {
    34  	val                 *T
    35  	toProtoreflectValue func(T) protoreflect.Value
    36  }
    37  
    38  func newSimpleValue[T any](val *T, toProtoreflectValue func(T) protoreflect.Value) HasValue {
    39  	return simpleValue[T]{val: val, toProtoreflectValue: toProtoreflectValue}
    40  }
    41  
    42  func (v simpleValue[T]) Get(protoreflect.Value) (protoreflect.Value, error) {
    43  	return v.toProtoreflectValue(*v.val), nil
    44  }