-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolvalue.go
More file actions
41 lines (36 loc) · 850 Bytes
/
boolvalue.go
File metadata and controls
41 lines (36 loc) · 850 Bytes
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
package opts
import (
"fmt"
)
// Bool defines a bool option with the specified name and a default value of
// false. The argument b points to a bool variable that will store the value.
// Bool will panic if name is not valid or repeats an existing option.
func (g *Group) Bool(b *bool, name string) {
if err := validateName("Bool", name); err != nil {
panic(err)
}
*b = false
opt := &opt{
value: &value[bool]{
ptr: b,
convert: toBool,
},
name: name,
isBool: true,
}
if err := g.optAlreadySet(name); err != nil {
panic(err)
}
g.opts[name] = opt
}
func toBool(s string) (bool, error) {
switch s {
case "true":
return true, nil
case "false":
return false, nil
default:
// Omit "opts: " since the caller will provide context.
return false, fmt.Errorf("bool value must be %q or %q", "true", "false")
}
}