gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

string_to_int64.go (5041B)


      1 package pflag
      2 
      3 import (
      4 	"bytes"
      5 	"fmt"
      6 	"strconv"
      7 	"strings"
      8 )
      9 
     10 // -- stringToInt64 Value
     11 type stringToInt64Value struct {
     12 	value   *map[string]int64
     13 	changed bool
     14 }
     15 
     16 func newStringToInt64Value(val map[string]int64, p *map[string]int64) *stringToInt64Value {
     17 	ssv := new(stringToInt64Value)
     18 	ssv.value = p
     19 	*ssv.value = val
     20 	return ssv
     21 }
     22 
     23 // Format: a=1,b=2
     24 func (s *stringToInt64Value) Set(val string) error {
     25 	ss := strings.Split(val, ",")
     26 	out := make(map[string]int64, len(ss))
     27 	for _, pair := range ss {
     28 		kv := strings.SplitN(pair, "=", 2)
     29 		if len(kv) != 2 {
     30 			return fmt.Errorf("%s must be formatted as key=value", pair)
     31 		}
     32 		var err error
     33 		out[kv[0]], err = strconv.ParseInt(kv[1], 10, 64)
     34 		if err != nil {
     35 			return err
     36 		}
     37 	}
     38 	if !s.changed {
     39 		*s.value = out
     40 	} else {
     41 		for k, v := range out {
     42 			(*s.value)[k] = v
     43 		}
     44 	}
     45 	s.changed = true
     46 	return nil
     47 }
     48 
     49 func (s *stringToInt64Value) Type() string {
     50 	return "stringToInt64"
     51 }
     52 
     53 func (s *stringToInt64Value) String() string {
     54 	var buf bytes.Buffer
     55 	i := 0
     56 	for k, v := range *s.value {
     57 		if i > 0 {
     58 			buf.WriteRune(',')
     59 		}
     60 		buf.WriteString(k)
     61 		buf.WriteRune('=')
     62 		buf.WriteString(strconv.FormatInt(v, 10))
     63 		i++
     64 	}
     65 	return "[" + buf.String() + "]"
     66 }
     67 
     68 func stringToInt64Conv(val string) (interface{}, error) {
     69 	val = strings.Trim(val, "[]")
     70 	// An empty string would cause an empty map
     71 	if len(val) == 0 {
     72 		return map[string]int64{}, nil
     73 	}
     74 	ss := strings.Split(val, ",")
     75 	out := make(map[string]int64, len(ss))
     76 	for _, pair := range ss {
     77 		kv := strings.SplitN(pair, "=", 2)
     78 		if len(kv) != 2 {
     79 			return nil, fmt.Errorf("%s must be formatted as key=value", pair)
     80 		}
     81 		var err error
     82 		out[kv[0]], err = strconv.ParseInt(kv[1], 10, 64)
     83 		if err != nil {
     84 			return nil, err
     85 		}
     86 	}
     87 	return out, nil
     88 }
     89 
     90 // GetStringToInt64 return the map[string]int64 value of a flag with the given name
     91 func (f *FlagSet) GetStringToInt64(name string) (map[string]int64, error) {
     92 	val, err := f.getFlagType(name, "stringToInt64", stringToInt64Conv)
     93 	if err != nil {
     94 		return map[string]int64{}, err
     95 	}
     96 	return val.(map[string]int64), nil
     97 }
     98 
     99 // StringToInt64Var defines a string flag with specified name, default value, and usage string.
    100 // The argument p point64s to a map[string]int64 variable in which to store the values of the multiple flags.
    101 // The value of each argument will not try to be separated by comma
    102 func (f *FlagSet) StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string) {
    103 	f.VarP(newStringToInt64Value(value, p), name, "", usage)
    104 }
    105 
    106 // StringToInt64VarP is like StringToInt64Var, but accepts a shorthand letter that can be used after a single dash.
    107 func (f *FlagSet) StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string) {
    108 	f.VarP(newStringToInt64Value(value, p), name, shorthand, usage)
    109 }
    110 
    111 // StringToInt64Var defines a string flag with specified name, default value, and usage string.
    112 // The argument p point64s to a map[string]int64 variable in which to store the value of the flag.
    113 // The value of each argument will not try to be separated by comma
    114 func StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string) {
    115 	CommandLine.VarP(newStringToInt64Value(value, p), name, "", usage)
    116 }
    117 
    118 // StringToInt64VarP is like StringToInt64Var, but accepts a shorthand letter that can be used after a single dash.
    119 func StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string) {
    120 	CommandLine.VarP(newStringToInt64Value(value, p), name, shorthand, usage)
    121 }
    122 
    123 // StringToInt64 defines a string flag with specified name, default value, and usage string.
    124 // The return value is the address of a map[string]int64 variable that stores the value of the flag.
    125 // The value of each argument will not try to be separated by comma
    126 func (f *FlagSet) StringToInt64(name string, value map[string]int64, usage string) *map[string]int64 {
    127 	p := map[string]int64{}
    128 	f.StringToInt64VarP(&p, name, "", value, usage)
    129 	return &p
    130 }
    131 
    132 // StringToInt64P is like StringToInt64, but accepts a shorthand letter that can be used after a single dash.
    133 func (f *FlagSet) StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64 {
    134 	p := map[string]int64{}
    135 	f.StringToInt64VarP(&p, name, shorthand, value, usage)
    136 	return &p
    137 }
    138 
    139 // StringToInt64 defines a string flag with specified name, default value, and usage string.
    140 // The return value is the address of a map[string]int64 variable that stores the value of the flag.
    141 // The value of each argument will not try to be separated by comma
    142 func StringToInt64(name string, value map[string]int64, usage string) *map[string]int64 {
    143 	return CommandLine.StringToInt64P(name, "", value, usage)
    144 }
    145 
    146 // StringToInt64P is like StringToInt64, but accepts a shorthand letter that can be used after a single dash.
    147 func StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64 {
    148 	return CommandLine.StringToInt64P(name, shorthand, value, usage)
    149 }