string_slice.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  75. // For example:
  76. // --ss="v1,v2" -ss="v3"
  77. // will result in
  78. // []string{"v1", "v2", "v3"}
  79. func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
  80. f.VarP(newStringSliceValue(value, p), name, "", usage)
  81. }
  82. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
  83. func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
  84. f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
  85. }
  86. // StringSliceVar defines a string flag with specified name, default value, and usage string.
  87. // The argument p points to a []string variable in which to store the value of the flag.
  88. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  89. // For example:
  90. // --ss="v1,v2" -ss="v3"
  91. // will result in
  92. // []string{"v1", "v2", "v3"}
  93. func StringSliceVar(p *[]string, name string, value []string, usage string) {
  94. CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
  95. }
  96. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
  97. func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
  98. CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
  99. }
  100. // StringSlice defines a string flag with specified name, default value, and usage string.
  101. // The return value is the address of a []string variable that stores the value of the flag.
  102. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  103. // For example:
  104. // --ss="v1,v2" -ss="v3"
  105. // will result in
  106. // []string{"v1", "v2", "v3"}
  107. func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
  108. p := []string{}
  109. f.StringSliceVarP(&p, name, "", value, usage)
  110. return &p
  111. }
  112. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
  113. func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
  114. p := []string{}
  115. f.StringSliceVarP(&p, name, shorthand, value, usage)
  116. return &p
  117. }
  118. // StringSlice defines a string flag with specified name, default value, and usage string.
  119. // The return value is the address of a []string variable that stores the value of the flag.
  120. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  121. // For example:
  122. // --ss="v1,v2" -ss="v3"
  123. // will result in
  124. // []string{"v1", "v2", "v3"}
  125. func StringSlice(name string, value []string, usage string) *[]string {
  126. return CommandLine.StringSliceP(name, "", value, usage)
  127. }
  128. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
  129. func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
  130. return CommandLine.StringSliceP(name, shorthand, value, usage)
  131. }