string_flag.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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 util
  14. // StringFlag is a string flag compatible with flags and pflags that keeps track of whether it had a value supplied or not.
  15. type StringFlag struct {
  16. // If Set has been invoked this value is true
  17. provided bool
  18. // The exact value provided on the flag
  19. value string
  20. }
  21. func NewStringFlag(defaultVal string) StringFlag {
  22. return StringFlag{value: defaultVal}
  23. }
  24. func (f *StringFlag) Default(value string) {
  25. f.value = value
  26. }
  27. func (f StringFlag) String() string {
  28. return f.value
  29. }
  30. func (f StringFlag) Value() string {
  31. return f.value
  32. }
  33. func (f *StringFlag) Set(value string) error {
  34. f.value = value
  35. f.provided = true
  36. return nil
  37. }
  38. func (f StringFlag) Provided() bool {
  39. return f.provided
  40. }
  41. func (f *StringFlag) Type() string {
  42. return "string"
  43. }