string_slice.go 4.0 KB

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