vanity

vanity - A tiny server for golang vanity redirects
Log | Files | Refs | README | LICENSE

string_array.go (4406B)


      1 package pflag
      2 
      3 // -- stringArray Value
      4 type stringArrayValue struct {
      5 	value   *[]string
      6 	changed bool
      7 }
      8 
      9 func newStringArrayValue(val []string, p *[]string) *stringArrayValue {
     10 	ssv := new(stringArrayValue)
     11 	ssv.value = p
     12 	*ssv.value = val
     13 	return ssv
     14 }
     15 
     16 func (s *stringArrayValue) Set(val string) error {
     17 	if !s.changed {
     18 		*s.value = []string{val}
     19 		s.changed = true
     20 	} else {
     21 		*s.value = append(*s.value, val)
     22 	}
     23 	return nil
     24 }
     25 
     26 func (s *stringArrayValue) Append(val string) error {
     27 	*s.value = append(*s.value, val)
     28 	return nil
     29 }
     30 
     31 func (s *stringArrayValue) Replace(val []string) error {
     32 	out := make([]string, len(val))
     33 	for i, d := range val {
     34 		var err error
     35 		out[i] = d
     36 		if err != nil {
     37 			return err
     38 		}
     39 	}
     40 	*s.value = out
     41 	return nil
     42 }
     43 
     44 func (s *stringArrayValue) GetSlice() []string {
     45 	out := make([]string, len(*s.value))
     46 	for i, d := range *s.value {
     47 		out[i] = d
     48 	}
     49 	return out
     50 }
     51 
     52 func (s *stringArrayValue) Type() string {
     53 	return "stringArray"
     54 }
     55 
     56 func (s *stringArrayValue) String() string {
     57 	str, _ := writeAsCSV(*s.value)
     58 	return "[" + str + "]"
     59 }
     60 
     61 func stringArrayConv(sval string) (interface{}, error) {
     62 	sval = sval[1 : len(sval)-1]
     63 	// An empty string would cause a array with one (empty) string
     64 	if len(sval) == 0 {
     65 		return []string{}, nil
     66 	}
     67 	return readAsCSV(sval)
     68 }
     69 
     70 // GetStringArray return the []string value of a flag with the given name
     71 func (f *FlagSet) GetStringArray(name string) ([]string, error) {
     72 	val, err := f.getFlagType(name, "stringArray", stringArrayConv)
     73 	if err != nil {
     74 		return []string{}, err
     75 	}
     76 	return val.([]string), nil
     77 }
     78 
     79 // StringArrayVar defines a string flag with specified name, default value, and usage string.
     80 // The argument p points to a []string variable in which to store the values of the multiple flags.
     81 // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
     82 func (f *FlagSet) StringArrayVar(p *[]string, name string, value []string, usage string) {
     83 	f.VarP(newStringArrayValue(value, p), name, "", usage)
     84 }
     85 
     86 // StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash.
     87 func (f *FlagSet) StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) {
     88 	f.VarP(newStringArrayValue(value, p), name, shorthand, usage)
     89 }
     90 
     91 // StringArrayVar defines a string flag with specified name, default value, and usage string.
     92 // The argument p points to a []string variable in which to store the value of the flag.
     93 // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
     94 func StringArrayVar(p *[]string, name string, value []string, usage string) {
     95 	CommandLine.VarP(newStringArrayValue(value, p), name, "", usage)
     96 }
     97 
     98 // StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash.
     99 func StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) {
    100 	CommandLine.VarP(newStringArrayValue(value, p), name, shorthand, usage)
    101 }
    102 
    103 // StringArray defines a string flag with specified name, default value, and usage string.
    104 // The return value is the address of a []string variable that stores the value of the flag.
    105 // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
    106 func (f *FlagSet) StringArray(name string, value []string, usage string) *[]string {
    107 	p := []string{}
    108 	f.StringArrayVarP(&p, name, "", value, usage)
    109 	return &p
    110 }
    111 
    112 // StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash.
    113 func (f *FlagSet) StringArrayP(name, shorthand string, value []string, usage string) *[]string {
    114 	p := []string{}
    115 	f.StringArrayVarP(&p, name, shorthand, value, usage)
    116 	return &p
    117 }
    118 
    119 // StringArray defines a string flag with specified name, default value, and usage string.
    120 // The return value is the address of a []string variable that stores the value of the flag.
    121 // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
    122 func StringArray(name string, value []string, usage string) *[]string {
    123 	return CommandLine.StringArrayP(name, "", value, usage)
    124 }
    125 
    126 // StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash.
    127 func StringArrayP(name, shorthand string, value []string, usage string) *[]string {
    128 	return CommandLine.StringArrayP(name, shorthand, value, usage)
    129 }