stringlist.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Copyright 2017 Google Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package flags defines extra flag types for use in command line flag parsing.
  14. package flags
  15. import (
  16. "strings"
  17. )
  18. // StringList is a []string container for command line flags. The default
  19. // initialization will create an empty slice. If you need default backing slice
  20. // map use NewStringList.
  21. type StringList []string
  22. func (ss *StringList) String() string {
  23. return strings.Join(*ss, ",")
  24. }
  25. // Get returns the values of ss. The interface will need to be type asserted to
  26. // StringList for use.
  27. func (ss *StringList) Get() interface{} {
  28. return *ss
  29. }
  30. // Set sets the value of ss to the comma separated values in s.
  31. func (ss *StringList) Set(s string) error {
  32. *ss = StringList(strings.Split(s, ","))
  33. return nil
  34. }
  35. // NewStringList will wrap the pointer to the slice in a StringList and set the
  36. // underlying slice to val.
  37. func NewStringList(p *[]string, val []string) *StringList {
  38. *p = val
  39. return (*StringList)(p)
  40. }