github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/bool_flag.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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 util
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  )
    23  
    24  // BoolFlag is a boolean flag compatible with flags and pflags that keeps track of whether it had a value supplied or not.
    25  // Beware!  If you use this type, you must actually specify --flag-name=true, you cannot leave it as --flag-name and still have
    26  // the value set
    27  type BoolFlag struct {
    28  	// If Set has been invoked this value is true
    29  	provided bool
    30  	// The exact value provided on the flag
    31  	value bool
    32  }
    33  
    34  func (f *BoolFlag) Default(value bool) {
    35  	f.value = value
    36  }
    37  
    38  func (f BoolFlag) String() string {
    39  	return fmt.Sprintf("%t", f.value)
    40  }
    41  
    42  func (f BoolFlag) Value() bool {
    43  	return f.value
    44  }
    45  
    46  func (f *BoolFlag) Set(value string) error {
    47  	boolVal, err := strconv.ParseBool(value)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	f.value = boolVal
    53  	f.provided = true
    54  
    55  	return nil
    56  }
    57  
    58  func (f BoolFlag) Provided() bool {
    59  	return f.provided
    60  }
    61  
    62  func (f *BoolFlag) Type() string {
    63  	return "bool"
    64  }