flag.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 The Bazel Authors. All rights reserved.
  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 flag
  15. import (
  16. stdflag "flag"
  17. "strings"
  18. )
  19. // MultiFlag allows repeated string flags to be collected into a slice
  20. type MultiFlag struct {
  21. Values *[]string
  22. }
  23. var _ stdflag.Value = (*MultiFlag)(nil)
  24. func (m *MultiFlag) Set(v string) error {
  25. *m.Values = append(*m.Values, v)
  26. return nil
  27. }
  28. func (m *MultiFlag) String() string {
  29. if m == nil || m.Values == nil {
  30. return ""
  31. }
  32. return strings.Join(*m.Values, ",")
  33. }
  34. // ExplicitFlag is a string flag that tracks whether it was set.
  35. type ExplicitFlag struct {
  36. IsSet *bool
  37. Value *string
  38. }
  39. var _ stdflag.Value = (*ExplicitFlag)(nil)
  40. func (f *ExplicitFlag) Set(value string) error {
  41. *f.IsSet = true
  42. *f.Value = value
  43. return nil
  44. }
  45. func (f *ExplicitFlag) String() string {
  46. if f == nil || f.Value == nil {
  47. return ""
  48. }
  49. return *f.Value
  50. }