string_slice.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package pflag
  2. import (
  3. "encoding/csv"
  4. "fmt"
  5. "strings"
  6. )
  7. var _ = fmt.Fprint
  8. // -- stringSlice Value
  9. type stringSliceValue struct {
  10. value *[]string
  11. changed bool
  12. }
  13. func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
  14. ssv := new(stringSliceValue)
  15. ssv.value = p
  16. *ssv.value = val
  17. return ssv
  18. }
  19. func (s *stringSliceValue) Set(val string) error {
  20. stringReader := strings.NewReader(val)
  21. csvReader := csv.NewReader(stringReader)
  22. v, err := csvReader.Read()
  23. if err != nil {
  24. return err
  25. }
  26. if !s.changed {
  27. *s.value = v
  28. } else {
  29. *s.value = append(*s.value, v...)
  30. }
  31. s.changed = true
  32. return nil
  33. }
  34. func (s *stringSliceValue) Type() string {
  35. return "stringSlice"
  36. }
  37. func (s *stringSliceValue) String() string { return "[" + strings.Join(*s.value, ",") + "]" }
  38. func stringSliceConv(sval string) (interface{}, error) {
  39. sval = strings.Trim(sval, "[]")
  40. // An empty string would cause a slice with one (empty) string
  41. if len(sval) == 0 {
  42. return []string{}, nil
  43. }
  44. v := strings.Split(sval, ",")
  45. return v, nil
  46. }
  47. // GetStringSlice return the []string value of a flag with the given name
  48. func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
  49. val, err := f.getFlagType(name, "stringSlice", stringSliceConv)
  50. if err != nil {
  51. return []string{}, err
  52. }
  53. return val.([]string), nil
  54. }
  55. // StringSliceVar defines a string flag with specified name, default value, and usage string.
  56. // The argument p points to a []string variable in which to store the value of the flag.
  57. func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
  58. f.VarP(newStringSliceValue(value, p), name, "", usage)
  59. }
  60. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
  61. func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
  62. f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
  63. }
  64. // StringSliceVar defines a string flag with specified name, default value, and usage string.
  65. // The argument p points to a []string variable in which to store the value of the flag.
  66. func StringSliceVar(p *[]string, name string, value []string, usage string) {
  67. CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
  68. }
  69. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
  70. func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
  71. CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
  72. }
  73. // StringSlice defines a string flag with specified name, default value, and usage string.
  74. // The return value is the address of a []string variable that stores the value of the flag.
  75. func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
  76. p := []string{}
  77. f.StringSliceVarP(&p, name, "", value, usage)
  78. return &p
  79. }
  80. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
  81. func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
  82. p := []string{}
  83. f.StringSliceVarP(&p, name, shorthand, value, usage)
  84. return &p
  85. }
  86. // StringSlice defines a string flag with specified name, default value, and usage string.
  87. // The return value is the address of a []string variable that stores the value of the flag.
  88. func StringSlice(name string, value []string, usage string) *[]string {
  89. return CommandLine.StringSliceP(name, "", value, usage)
  90. }
  91. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
  92. func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
  93. return CommandLine.StringSliceP(name, shorthand, value, usage)
  94. }