blob: cf16376bf9f03e8d378093514763cc8c6cf824de [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package flag
18
19import (
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.
26type Tristate int
27
28const (
29 Unset Tristate = iota // 0
30 True
31 False
32)
33
34func (f *Tristate) Default(value bool) {
35 *f = triFromBool(value)
36}
37
38func (f Tristate) String() string {
39 b := boolFromTri(f)
40 return fmt.Sprintf("%t", b)
41}
42
43func (f Tristate) Value() bool {
44 b := boolFromTri(f)
45 return b
46}
47
48func (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
58func (f Tristate) Provided() bool {
59 if f != Unset {
60 return true
61 }
62 return false
63}
64
65func (f *Tristate) Type() string {
66 return "tristate"
67}
68
69func boolFromTri(t Tristate) bool {
70 if t == True {
71 return true
72 } else {
73 return false
74 }
75}
76
77func triFromBool(b bool) Tristate {
78 if b {
79 return True
80 } else {
81 return False
82 }
83}