strings.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package flags
  15. import (
  16. "errors"
  17. "flag"
  18. "sort"
  19. "strings"
  20. )
  21. // NewStringsFlag creates a new string flag for which any one of the given
  22. // strings is a valid value, and any other value is an error.
  23. //
  24. // valids[0] will be default value. Caller must be sure len(valids)!=0 or
  25. // it will panic.
  26. func NewStringsFlag(valids ...string) *StringsFlag {
  27. return &StringsFlag{Values: valids, val: valids[0]}
  28. }
  29. // StringsFlag implements the flag.Value interface.
  30. type StringsFlag struct {
  31. Values []string
  32. val string
  33. }
  34. // Set verifies the argument to be a valid member of the allowed values
  35. // before setting the underlying flag value.
  36. func (ss *StringsFlag) Set(s string) error {
  37. for _, v := range ss.Values {
  38. if s == v {
  39. ss.val = s
  40. return nil
  41. }
  42. }
  43. return errors.New("invalid value")
  44. }
  45. // String returns the set value (if any) of the StringsFlag
  46. func (ss *StringsFlag) String() string {
  47. return ss.val
  48. }
  49. // StringsValueV2 wraps "sort.StringSlice".
  50. type StringsValueV2 sort.StringSlice
  51. // Set parses a command line set of strings, separated by comma.
  52. // Implements "flag.Value" interface.
  53. func (ss *StringsValueV2) Set(s string) error {
  54. *ss = strings.Split(s, ",")
  55. return nil
  56. }
  57. // String implements "flag.Value" interface.
  58. func (ss *StringsValueV2) String() string { return strings.Join(*ss, ",") }
  59. // NewStringsValueV2 implements string slice as "flag.Value" interface.
  60. // Given value is to be separated by comma.
  61. func NewStringsValueV2(s string) (ss *StringsValueV2) {
  62. if s == "" {
  63. return &StringsValueV2{}
  64. }
  65. ss = new(StringsValueV2)
  66. if err := ss.Set(s); err != nil {
  67. plog.Panicf("new StringsValueV2 should never fail: %v", err)
  68. }
  69. return ss
  70. }
  71. // StringsFromFlagV2 returns a string slice from the flag.
  72. func StringsFromFlagV2(fs *flag.FlagSet, flagName string) []string {
  73. return []string(*fs.Lookup(flagName).Value.(*StringsValueV2))
  74. }