1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package flag 18 19 // StringFlag is a string flag compatible with flags and pflags that keeps track of whether it had a value supplied or not. 20 type StringFlag struct { 21 // If Set has been invoked this value is true 22 provided bool 23 // The exact value provided on the flag 24 value string 25 } 26 27 func NewStringFlag(defaultVal string) StringFlag { 28 return StringFlag{value: defaultVal} 29 } 30 31 func (f *StringFlag) Default(value string) { 32 f.value = value 33 } 34 35 func (f StringFlag) String() string { 36 return f.value 37 } 38 39 func (f StringFlag) Value() string { 40 return f.value 41 } 42 43 func (f *StringFlag) Set(value string) error { 44 f.value = value 45 f.provided = true 46 47 return nil 48 } 49 50 func (f StringFlag) Provided() bool { 51 return f.provided 52 } 53 54 func (f *StringFlag) Type() string { 55 return "string" 56 } 57