1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // This value can be a boolean ("true", "false") or "maybe"
14 type triStateValue int
17 triStateFalse triStateValue = 0
18 triStateTrue triStateValue = 1
19 triStateMaybe triStateValue = 2
22 const strTriStateMaybe = "maybe"
24 func (v *triStateValue) IsBoolFlag() bool {
28 func (v *triStateValue) Get() interface{} {
29 return triStateValue(*v)
32 func (v *triStateValue) Set(s string) error {
33 if s == strTriStateMaybe {
37 boolVal, err := strconv.ParseBool(s)
46 func (v *triStateValue) String() string {
47 if *v == triStateMaybe {
48 return strTriStateMaybe
50 return strconv.FormatBool(*v == triStateTrue)
53 // The type of the flag as required by the pflag.Value interface
54 func (v *triStateValue) Type() string {
58 func setUpFlagSet(tristate *triStateValue) *FlagSet {
59 f := NewFlagSet("test", ContinueOnError)
60 *tristate = triStateFalse
61 flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
62 flag.NoOptDefVal = "true"
66 func TestExplicitTrue(t *testing.T) {
67 var tristate triStateValue
68 f := setUpFlagSet(&tristate)
69 err := f.Parse([]string{"--tristate=true"})
71 t.Fatal("expected no error; got", err)
73 if tristate != triStateTrue {
74 t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
78 func TestImplicitTrue(t *testing.T) {
79 var tristate triStateValue
80 f := setUpFlagSet(&tristate)
81 err := f.Parse([]string{"--tristate"})
83 t.Fatal("expected no error; got", err)
85 if tristate != triStateTrue {
86 t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
90 func TestShortFlag(t *testing.T) {
91 var tristate triStateValue
92 f := setUpFlagSet(&tristate)
93 err := f.Parse([]string{"-t"})
95 t.Fatal("expected no error; got", err)
97 if tristate != triStateTrue {
98 t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
102 func TestShortFlagExtraArgument(t *testing.T) {
103 var tristate triStateValue
104 f := setUpFlagSet(&tristate)
105 // The"maybe"turns into an arg, since short boolean options will only do true/false
106 err := f.Parse([]string{"-t", "maybe"})
108 t.Fatal("expected no error; got", err)
110 if tristate != triStateTrue {
111 t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
114 if len(args) != 1 || args[0] != "maybe" {
115 t.Fatal("expected an extra 'maybe' argument to stick around")
119 func TestExplicitMaybe(t *testing.T) {
120 var tristate triStateValue
121 f := setUpFlagSet(&tristate)
122 err := f.Parse([]string{"--tristate=maybe"})
124 t.Fatal("expected no error; got", err)
126 if tristate != triStateMaybe {
127 t.Fatal("expected", triStateMaybe, "(triStateMaybe) but got", tristate, "instead")
131 func TestExplicitFalse(t *testing.T) {
132 var tristate triStateValue
133 f := setUpFlagSet(&tristate)
134 err := f.Parse([]string{"--tristate=false"})
136 t.Fatal("expected no error; got", err)
138 if tristate != triStateFalse {
139 t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
143 func TestImplicitFalse(t *testing.T) {
144 var tristate triStateValue
145 f := setUpFlagSet(&tristate)
146 err := f.Parse([]string{})
148 t.Fatal("expected no error; got", err)
150 if tristate != triStateFalse {
151 t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
155 func TestInvalidValue(t *testing.T) {
156 var tristate triStateValue
157 f := setUpFlagSet(&tristate)
160 err := f.Parse([]string{"--tristate=invalid"})
162 t.Fatal("expected an error but did not get any, tristate has value", tristate)
166 func TestBoolP(t *testing.T) {
167 b := BoolP("bool", "b", false, "bool value in CommandLine")
168 c := BoolP("c", "c", false, "other bool value")
169 args := []string{"--bool"}
170 if err := CommandLine.Parse(args); err != nil {
171 t.Error("expected no error, got ", err)
174 t.Errorf("expected b=true got b=%v", *b)
177 t.Errorf("expect c=false got c=%v", *c)