-
Notifications
You must be signed in to change notification settings - Fork 0
/
tristate.go
71 lines (57 loc) · 1.14 KB
/
tristate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2023 Emory.Du <orangeduxiaocheng@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package flag
import (
"fmt"
"github.com/spf13/pflag"
"strconv"
)
// Tristate is a flag compatible with flags and pflags that
// keeps track of whether it had a value supplied or not.
type Tristate int
const (
Unset Tristate = iota
True
False
)
var _ pflag.Value = (*Tristate)(nil)
func (f *Tristate) Default(value bool) {
*f = triFromBool(value)
}
func (f *Tristate) String() string {
b := boolFromTri(*f)
return fmt.Sprintf("%t", b)
}
func (f *Tristate) Value() bool {
b := boolFromTri(*f)
return b
}
func (f *Tristate) Set(value string) error {
boolVal, err := strconv.ParseBool(value)
if err != nil {
return err
}
*f = triFromBool(boolVal)
return nil
}
func (f *Tristate) Provided() bool {
return *f != Unset
}
func (f *Tristate) Type() string {
return "tristate"
}
func triFromBool(b bool) Tristate {
if b {
return True
} else {
return False
}
}
func boolFromTri(t Tristate) bool {
if t == True {
return true
} else {
return false
}
}