本站源代码
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.

161 lines
5.2KB

  1. package pflag
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "fmt"
  6. "strings"
  7. )
  8. // -- stringToString Value
  9. type stringToStringValue struct {
  10. value *map[string]string
  11. changed bool
  12. }
  13. func newStringToStringValue(val map[string]string, p *map[string]string) *stringToStringValue {
  14. ssv := new(stringToStringValue)
  15. ssv.value = p
  16. *ssv.value = val
  17. return ssv
  18. }
  19. // Format: a=1,b=2
  20. func (s *stringToStringValue) Set(val string) error {
  21. var ss []string
  22. n := strings.Count(val, "=")
  23. switch n {
  24. case 0:
  25. return fmt.Errorf("%s must be formatted as key=value", val)
  26. case 1:
  27. ss = append(ss, strings.Trim(val, `"`))
  28. default:
  29. r := csv.NewReader(strings.NewReader(val))
  30. var err error
  31. ss, err = r.Read()
  32. if err != nil {
  33. return err
  34. }
  35. }
  36. out := make(map[string]string, len(ss))
  37. for _, pair := range ss {
  38. kv := strings.SplitN(pair, "=", 2)
  39. if len(kv) != 2 {
  40. return fmt.Errorf("%s must be formatted as key=value", pair)
  41. }
  42. out[kv[0]] = kv[1]
  43. }
  44. if !s.changed {
  45. *s.value = out
  46. } else {
  47. for k, v := range out {
  48. (*s.value)[k] = v
  49. }
  50. }
  51. s.changed = true
  52. return nil
  53. }
  54. func (s *stringToStringValue) Type() string {
  55. return "stringToString"
  56. }
  57. func (s *stringToStringValue) String() string {
  58. records := make([]string, 0, len(*s.value)>>1)
  59. for k, v := range *s.value {
  60. records = append(records, k+"="+v)
  61. }
  62. var buf bytes.Buffer
  63. w := csv.NewWriter(&buf)
  64. if err := w.Write(records); err != nil {
  65. panic(err)
  66. }
  67. w.Flush()
  68. return "[" + strings.TrimSpace(buf.String()) + "]"
  69. }
  70. func stringToStringConv(val string) (interface{}, error) {
  71. val = strings.Trim(val, "[]")
  72. // An empty string would cause an empty map
  73. if len(val) == 0 {
  74. return map[string]string{}, nil
  75. }
  76. r := csv.NewReader(strings.NewReader(val))
  77. ss, err := r.Read()
  78. if err != nil {
  79. return nil, err
  80. }
  81. out := make(map[string]string, len(ss))
  82. for _, pair := range ss {
  83. kv := strings.SplitN(pair, "=", 2)
  84. if len(kv) != 2 {
  85. return nil, fmt.Errorf("%s must be formatted as key=value", pair)
  86. }
  87. out[kv[0]] = kv[1]
  88. }
  89. return out, nil
  90. }
  91. // GetStringToString return the map[string]string value of a flag with the given name
  92. func (f *FlagSet) GetStringToString(name string) (map[string]string, error) {
  93. val, err := f.getFlagType(name, "stringToString", stringToStringConv)
  94. if err != nil {
  95. return map[string]string{}, err
  96. }
  97. return val.(map[string]string), nil
  98. }
  99. // StringToStringVar defines a string flag with specified name, default value, and usage string.
  100. // The argument p points to a map[string]string variable in which to store the values of the multiple flags.
  101. // The value of each argument will not try to be separated by comma
  102. func (f *FlagSet) StringToStringVar(p *map[string]string, name string, value map[string]string, usage string) {
  103. f.VarP(newStringToStringValue(value, p), name, "", usage)
  104. }
  105. // StringToStringVarP is like StringToStringVar, but accepts a shorthand letter that can be used after a single dash.
  106. func (f *FlagSet) StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string) {
  107. f.VarP(newStringToStringValue(value, p), name, shorthand, usage)
  108. }
  109. // StringToStringVar defines a string flag with specified name, default value, and usage string.
  110. // The argument p points to a map[string]string variable in which to store the value of the flag.
  111. // The value of each argument will not try to be separated by comma
  112. func StringToStringVar(p *map[string]string, name string, value map[string]string, usage string) {
  113. CommandLine.VarP(newStringToStringValue(value, p), name, "", usage)
  114. }
  115. // StringToStringVarP is like StringToStringVar, but accepts a shorthand letter that can be used after a single dash.
  116. func StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string) {
  117. CommandLine.VarP(newStringToStringValue(value, p), name, shorthand, usage)
  118. }
  119. // StringToString defines a string flag with specified name, default value, and usage string.
  120. // The return value is the address of a map[string]string variable that stores the value of the flag.
  121. // The value of each argument will not try to be separated by comma
  122. func (f *FlagSet) StringToString(name string, value map[string]string, usage string) *map[string]string {
  123. p := map[string]string{}
  124. f.StringToStringVarP(&p, name, "", value, usage)
  125. return &p
  126. }
  127. // StringToStringP is like StringToString, but accepts a shorthand letter that can be used after a single dash.
  128. func (f *FlagSet) StringToStringP(name, shorthand string, value map[string]string, usage string) *map[string]string {
  129. p := map[string]string{}
  130. f.StringToStringVarP(&p, name, shorthand, value, usage)
  131. return &p
  132. }
  133. // StringToString defines a string flag with specified name, default value, and usage string.
  134. // The return value is the address of a map[string]string variable that stores the value of the flag.
  135. // The value of each argument will not try to be separated by comma
  136. func StringToString(name string, value map[string]string, usage string) *map[string]string {
  137. return CommandLine.StringToStringP(name, "", value, usage)
  138. }
  139. // StringToStringP is like StringToString, but accepts a shorthand letter that can be used after a single dash.
  140. func StringToStringP(name, shorthand string, value map[string]string, usage string) *map[string]string {
  141. return CommandLine.StringToStringP(name, shorthand, value, usage)
  142. }
上海开阖软件有限公司 沪ICP备12045867号-1