...

Source file src/k8s.io/component-base/cli/flag/tristate.go

Documentation: k8s.io/component-base/cli/flag

     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  import (
    20  	"fmt"
    21  	"strconv"
    22  )
    23  
    24  // Tristate is a flag compatible with flags and pflags that
    25  // keeps track of whether it had a value supplied or not.
    26  type Tristate int
    27  
    28  const (
    29  	Unset Tristate = iota // 0
    30  	True
    31  	False
    32  )
    33  
    34  func (f *Tristate) Default(value bool) {
    35  	*f = triFromBool(value)
    36  }
    37  
    38  func (f Tristate) String() string {
    39  	b := boolFromTri(f)
    40  	return fmt.Sprintf("%t", b)
    41  }
    42  
    43  func (f Tristate) Value() bool {
    44  	b := boolFromTri(f)
    45  	return b
    46  }
    47  
    48  func (f *Tristate) Set(value string) error {
    49  	boolVal, err := strconv.ParseBool(value)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	*f = triFromBool(boolVal)
    55  	return nil
    56  }
    57  
    58  func (f Tristate) Provided() bool {
    59  	if f != Unset {
    60  		return true
    61  	}
    62  	return false
    63  }
    64  
    65  func (f *Tristate) Type() string {
    66  	return "tristate"
    67  }
    68  
    69  func boolFromTri(t Tristate) bool {
    70  	if t == True {
    71  		return true
    72  	} else {
    73  		return false
    74  	}
    75  }
    76  
    77  func triFromBool(b bool) Tristate {
    78  	if b {
    79  		return True
    80  	} else {
    81  		return False
    82  	}
    83  }
    84  

View as plain text