本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.8KB

  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) Type() string {
  23. return "stringArray"
  24. }
  25. func (s *stringArrayValue) String() string {
  26. str, _ := writeAsCSV(*s.value)
  27. return "[" + str + "]"
  28. }
  29. func stringArrayConv(sval string) (interface{}, error) {
  30. sval = sval[1 : len(sval)-1]
  31. // An empty string would cause a array with one (empty) string
  32. if len(sval) == 0 {
  33. return []string{}, nil
  34. }
  35. return readAsCSV(sval)
  36. }
  37. // GetStringArray return the []string value of a flag with the given name
  38. func (f *FlagSet) GetStringArray(name string) ([]string, error) {
  39. val, err := f.getFlagType(name, "stringArray", stringArrayConv)
  40. if err != nil {
  41. return []string{}, err
  42. }
  43. return val.([]string), nil
  44. }
  45. // StringArrayVar defines a string flag with specified name, default value, and usage string.
  46. // The argument p points to a []string variable in which to store the values of the multiple flags.
  47. // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
  48. func (f *FlagSet) StringArrayVar(p *[]string, name string, value []string, usage string) {
  49. f.VarP(newStringArrayValue(value, p), name, "", usage)
  50. }
  51. // StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash.
  52. func (f *FlagSet) StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) {
  53. f.VarP(newStringArrayValue(value, p), name, shorthand, usage)
  54. }
  55. // StringArrayVar 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. // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
  58. func StringArrayVar(p *[]string, name string, value []string, usage string) {
  59. CommandLine.VarP(newStringArrayValue(value, p), name, "", usage)
  60. }
  61. // StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash.
  62. func StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) {
  63. CommandLine.VarP(newStringArrayValue(value, p), name, shorthand, usage)
  64. }
  65. // StringArray defines a string flag with specified name, default value, and usage string.
  66. // The return value is the address of a []string variable that stores the value of the flag.
  67. // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
  68. func (f *FlagSet) StringArray(name string, value []string, usage string) *[]string {
  69. p := []string{}
  70. f.StringArrayVarP(&p, name, "", value, usage)
  71. return &p
  72. }
  73. // StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash.
  74. func (f *FlagSet) StringArrayP(name, shorthand string, value []string, usage string) *[]string {
  75. p := []string{}
  76. f.StringArrayVarP(&p, name, shorthand, value, usage)
  77. return &p
  78. }
  79. // StringArray defines a string flag with specified name, default value, and usage string.
  80. // The return value is the address of a []string variable that stores the value of the flag.
  81. // The value of each argument will not try to be separated by comma. Use a StringSlice for that.
  82. func StringArray(name string, value []string, usage string) *[]string {
  83. return CommandLine.StringArrayP(name, "", value, usage)
  84. }
  85. // StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash.
  86. func StringArrayP(name, shorthand string, value []string, usage string) *[]string {
  87. return CommandLine.StringArrayP(name, shorthand, value, usage)
  88. }
上海开阖软件有限公司 沪ICP备12045867号-1