string_array.go 4.3 KB

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