vendor/github.com/spf13/pflag/uint_slice.go
changeset 242 2a9ec03fe5a1
child 251 1c52a0eeb952
equal deleted inserted replaced
241:e77dad242f4c 242:2a9ec03fe5a1
       
     1 package pflag
       
     2 
       
     3 import (
       
     4 	"fmt"
       
     5 	"strconv"
       
     6 	"strings"
       
     7 )
       
     8 
       
     9 // -- uintSlice Value
       
    10 type uintSliceValue struct {
       
    11 	value   *[]uint
       
    12 	changed bool
       
    13 }
       
    14 
       
    15 func newUintSliceValue(val []uint, p *[]uint) *uintSliceValue {
       
    16 	uisv := new(uintSliceValue)
       
    17 	uisv.value = p
       
    18 	*uisv.value = val
       
    19 	return uisv
       
    20 }
       
    21 
       
    22 func (s *uintSliceValue) Set(val string) error {
       
    23 	ss := strings.Split(val, ",")
       
    24 	out := make([]uint, len(ss))
       
    25 	for i, d := range ss {
       
    26 		u, err := strconv.ParseUint(d, 10, 0)
       
    27 		if err != nil {
       
    28 			return err
       
    29 		}
       
    30 		out[i] = uint(u)
       
    31 	}
       
    32 	if !s.changed {
       
    33 		*s.value = out
       
    34 	} else {
       
    35 		*s.value = append(*s.value, out...)
       
    36 	}
       
    37 	s.changed = true
       
    38 	return nil
       
    39 }
       
    40 
       
    41 func (s *uintSliceValue) Type() string {
       
    42 	return "uintSlice"
       
    43 }
       
    44 
       
    45 func (s *uintSliceValue) String() string {
       
    46 	out := make([]string, len(*s.value))
       
    47 	for i, d := range *s.value {
       
    48 		out[i] = fmt.Sprintf("%d", d)
       
    49 	}
       
    50 	return "[" + strings.Join(out, ",") + "]"
       
    51 }
       
    52 
       
    53 func uintSliceConv(val string) (interface{}, error) {
       
    54 	val = strings.Trim(val, "[]")
       
    55 	// Empty string would cause a slice with one (empty) entry
       
    56 	if len(val) == 0 {
       
    57 		return []uint{}, nil
       
    58 	}
       
    59 	ss := strings.Split(val, ",")
       
    60 	out := make([]uint, len(ss))
       
    61 	for i, d := range ss {
       
    62 		u, err := strconv.ParseUint(d, 10, 0)
       
    63 		if err != nil {
       
    64 			return nil, err
       
    65 		}
       
    66 		out[i] = uint(u)
       
    67 	}
       
    68 	return out, nil
       
    69 }
       
    70 
       
    71 // GetUintSlice returns the []uint value of a flag with the given name.
       
    72 func (f *FlagSet) GetUintSlice(name string) ([]uint, error) {
       
    73 	val, err := f.getFlagType(name, "uintSlice", uintSliceConv)
       
    74 	if err != nil {
       
    75 		return []uint{}, err
       
    76 	}
       
    77 	return val.([]uint), nil
       
    78 }
       
    79 
       
    80 // UintSliceVar defines a uintSlice flag with specified name, default value, and usage string.
       
    81 // The argument p points to a []uint variable in which to store the value of the flag.
       
    82 func (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string) {
       
    83 	f.VarP(newUintSliceValue(value, p), name, "", usage)
       
    84 }
       
    85 
       
    86 // UintSliceVarP is like UintSliceVar, but accepts a shorthand letter that can be used after a single dash.
       
    87 func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) {
       
    88 	f.VarP(newUintSliceValue(value, p), name, shorthand, usage)
       
    89 }
       
    90 
       
    91 // UintSliceVar defines a uint[] flag with specified name, default value, and usage string.
       
    92 // The argument p points to a uint[] variable in which to store the value of the flag.
       
    93 func UintSliceVar(p *[]uint, name string, value []uint, usage string) {
       
    94 	CommandLine.VarP(newUintSliceValue(value, p), name, "", usage)
       
    95 }
       
    96 
       
    97 // UintSliceVarP is like the UintSliceVar, but accepts a shorthand letter that can be used after a single dash.
       
    98 func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) {
       
    99 	CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage)
       
   100 }
       
   101 
       
   102 // UintSlice defines a []uint flag with specified name, default value, and usage string.
       
   103 // The return value is the address of a []uint variable that stores the value of the flag.
       
   104 func (f *FlagSet) UintSlice(name string, value []uint, usage string) *[]uint {
       
   105 	p := []uint{}
       
   106 	f.UintSliceVarP(&p, name, "", value, usage)
       
   107 	return &p
       
   108 }
       
   109 
       
   110 // UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash.
       
   111 func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string) *[]uint {
       
   112 	p := []uint{}
       
   113 	f.UintSliceVarP(&p, name, shorthand, value, usage)
       
   114 	return &p
       
   115 }
       
   116 
       
   117 // UintSlice defines a []uint flag with specified name, default value, and usage string.
       
   118 // The return value is the address of a []uint variable that stores the value of the flag.
       
   119 func UintSlice(name string, value []uint, usage string) *[]uint {
       
   120 	return CommandLine.UintSliceP(name, "", value, usage)
       
   121 }
       
   122 
       
   123 // UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash.
       
   124 func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint {
       
   125 	return CommandLine.UintSliceP(name, shorthand, value, usage)
       
   126 }