1package pflag
2
3import (
4	"fmt"
5	"strconv"
6)
7
8// optional interface to indicate boolean flags that can be
9// supplied without "=value" text
10type boolFlag interface {
11	Value
12	IsBoolFlag() bool
13}
14
15// -- bool Value
16type boolValue bool
17
18func newBoolValue(val bool, p *bool) *boolValue {
19	*p = val
20	return (*boolValue)(p)
21}
22
23func (b *boolValue) Set(s string) error {
24	v, err := strconv.ParseBool(s)
25	*b = boolValue(v)
26	return err
27}
28
29func (b *boolValue) Type() string {
30	return "bool"
31}
32
33func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
34
35func (b *boolValue) IsBoolFlag() bool { return true }
36
37func boolConv(sval string) (interface{}, error) {
38	return strconv.ParseBool(sval)
39}
40
41// GetBool return the bool value of a flag with the given name
42func (f *FlagSet) GetBool(name string) (bool, error) {
43	val, err := f.getFlagType(name, "bool", boolConv)
44	if err != nil {
45		return false, err
46	}
47	return val.(bool), nil
48}
49
50// BoolVar defines a bool flag with specified name, default value, and usage string.
51// The argument p points to a bool variable in which to store the value of the flag.
52func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
53	f.BoolVarP(p, name, "", value, usage)
54}
55
56// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
57func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
58	flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
59	flag.NoOptDefVal = "true"
60}
61
62// BoolVar defines a bool flag with specified name, default value, and usage string.
63// The argument p points to a bool variable in which to store the value of the flag.
64func BoolVar(p *bool, name string, value bool, usage string) {
65	BoolVarP(p, name, "", value, usage)
66}
67
68// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
69func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
70	flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
71	flag.NoOptDefVal = "true"
72}
73
74// Bool defines a bool flag with specified name, default value, and usage string.
75// The return value is the address of a bool variable that stores the value of the flag.
76func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
77	return f.BoolP(name, "", value, usage)
78}
79
80// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
81func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool {
82	p := new(bool)
83	f.BoolVarP(p, name, shorthand, value, usage)
84	return p
85}
86
87// Bool defines a bool flag with specified name, default value, and usage string.
88// The return value is the address of a bool variable that stores the value of the flag.
89func Bool(name string, value bool, usage string) *bool {
90	return BoolP(name, "", value, usage)
91}
92
93// BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
94func BoolP(name, shorthand string, value bool, usage string) *bool {
95	b := CommandLine.BoolP(name, shorthand, value, usage)
96	return b
97}
98