string_slice.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 (s *stringSliceValue) Append(val string) error {
  57. *s.value = append(*s.value, val)
  58. return nil
  59. }
  60. func (s *stringSliceValue) Replace(val []string) error {
  61. *s.value = val
  62. return nil
  63. }
  64. func (s *stringSliceValue) GetSlice() []string {
  65. return *s.value
  66. }
  67. func stringSliceConv(sval string) (interface{}, error) {
  68. sval = sval[1 : len(sval)-1]
  69. // An empty string would cause a slice with one (empty) string
  70. if len(sval) == 0 {
  71. return []string{}, nil
  72. }
  73. return readAsCSV(sval)
  74. }
  75. // GetStringSlice return the []string value of a flag with the given name
  76. func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
  77. val, err := f.getFlagType(name, "stringSlice", stringSliceConv)
  78. if err != nil {
  79. return []string{}, err
  80. }
  81. return val.([]string), nil
  82. }
  83. // StringSliceVar defines a string flag with specified name, default value, and usage string.
  84. // The argument p points to a []string variable in which to store the value of the flag.
  85. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  86. // For example:
  87. // --ss="v1,v2" --ss="v3"
  88. // will result in
  89. // []string{"v1", "v2", "v3"}
  90. func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
  91. f.VarP(newStringSliceValue(value, p), name, "", usage)
  92. }
  93. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
  94. func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
  95. f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
  96. }
  97. // StringSliceVar defines a string flag with specified name, default value, and usage string.
  98. // The argument p points to a []string variable in which to store the value of the flag.
  99. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  100. // For example:
  101. // --ss="v1,v2" --ss="v3"
  102. // will result in
  103. // []string{"v1", "v2", "v3"}
  104. func StringSliceVar(p *[]string, name string, value []string, usage string) {
  105. CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
  106. }
  107. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
  108. func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
  109. CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
  110. }
  111. // StringSlice defines a string flag with specified name, default value, and usage string.
  112. // The return value is the address of a []string variable that stores the value of the flag.
  113. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  114. // For example:
  115. // --ss="v1,v2" --ss="v3"
  116. // will result in
  117. // []string{"v1", "v2", "v3"}
  118. func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
  119. p := []string{}
  120. f.StringSliceVarP(&p, name, "", value, usage)
  121. return &p
  122. }
  123. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
  124. func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
  125. p := []string{}
  126. f.StringSliceVarP(&p, name, shorthand, value, usage)
  127. return &p
  128. }
  129. // StringSlice defines a string flag with specified name, default value, and usage string.
  130. // The return value is the address of a []string variable that stores the value of the flag.
  131. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
  132. // For example:
  133. // --ss="v1,v2" --ss="v3"
  134. // will result in
  135. // []string{"v1", "v2", "v3"}
  136. func StringSlice(name string, value []string, usage string) *[]string {
  137. return CommandLine.StringSliceP(name, "", value, usage)
  138. }
  139. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
  140. func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
  141. return CommandLine.StringSliceP(name, shorthand, value, usage)
  142. }