bool_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.
  4. package pflag
  5. import (
  6. "bytes"
  7. "fmt"
  8. "strconv"
  9. "testing"
  10. )
  11. // This value can be a boolean ("true", "false") or "maybe"
  12. type triStateValue int
  13. const (
  14. triStateFalse triStateValue = 0
  15. triStateTrue triStateValue = 1
  16. triStateMaybe triStateValue = 2
  17. )
  18. const strTriStateMaybe = "maybe"
  19. func (v *triStateValue) IsBoolFlag() bool {
  20. return true
  21. }
  22. func (v *triStateValue) Get() interface{} {
  23. return triStateValue(*v)
  24. }
  25. func (v *triStateValue) Set(s string) error {
  26. if s == strTriStateMaybe {
  27. *v = triStateMaybe
  28. return nil
  29. }
  30. boolVal, err := strconv.ParseBool(s)
  31. if boolVal {
  32. *v = triStateTrue
  33. } else {
  34. *v = triStateFalse
  35. }
  36. return err
  37. }
  38. func (v *triStateValue) String() string {
  39. if *v == triStateMaybe {
  40. return strTriStateMaybe
  41. }
  42. return fmt.Sprintf("%v", bool(*v == triStateTrue))
  43. }
  44. // The type of the flag as required by the pflag.Value interface
  45. func (v *triStateValue) Type() string {
  46. return "version"
  47. }
  48. func setUpFlagSet(tristate *triStateValue) *FlagSet {
  49. f := NewFlagSet("test", ContinueOnError)
  50. *tristate = triStateFalse
  51. flag := f.VarPF(tristate, "tristate", "t", "tristate value (true, maybe or false)")
  52. flag.NoOptDefVal = "true"
  53. return f
  54. }
  55. func TestExplicitTrue(t *testing.T) {
  56. var tristate triStateValue
  57. f := setUpFlagSet(&tristate)
  58. err := f.Parse([]string{"--tristate=true"})
  59. if err != nil {
  60. t.Fatal("expected no error; got", err)
  61. }
  62. if tristate != triStateTrue {
  63. t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
  64. }
  65. }
  66. func TestImplicitTrue(t *testing.T) {
  67. var tristate triStateValue
  68. f := setUpFlagSet(&tristate)
  69. err := f.Parse([]string{"--tristate"})
  70. if err != nil {
  71. t.Fatal("expected no error; got", err)
  72. }
  73. if tristate != triStateTrue {
  74. t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
  75. }
  76. }
  77. func TestShortFlag(t *testing.T) {
  78. var tristate triStateValue
  79. f := setUpFlagSet(&tristate)
  80. err := f.Parse([]string{"-t"})
  81. if err != nil {
  82. t.Fatal("expected no error; got", err)
  83. }
  84. if tristate != triStateTrue {
  85. t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
  86. }
  87. }
  88. func TestShortFlagExtraArgument(t *testing.T) {
  89. var tristate triStateValue
  90. f := setUpFlagSet(&tristate)
  91. // The"maybe"turns into an arg, since short boolean options will only do true/false
  92. err := f.Parse([]string{"-t", "maybe"})
  93. if err != nil {
  94. t.Fatal("expected no error; got", err)
  95. }
  96. if tristate != triStateTrue {
  97. t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead")
  98. }
  99. args := f.Args()
  100. if len(args) != 1 || args[0] != "maybe" {
  101. t.Fatal("expected an extra 'maybe' argument to stick around")
  102. }
  103. }
  104. func TestExplicitMaybe(t *testing.T) {
  105. var tristate triStateValue
  106. f := setUpFlagSet(&tristate)
  107. err := f.Parse([]string{"--tristate=maybe"})
  108. if err != nil {
  109. t.Fatal("expected no error; got", err)
  110. }
  111. if tristate != triStateMaybe {
  112. t.Fatal("expected", triStateMaybe, "(triStateMaybe) but got", tristate, "instead")
  113. }
  114. }
  115. func TestExplicitFalse(t *testing.T) {
  116. var tristate triStateValue
  117. f := setUpFlagSet(&tristate)
  118. err := f.Parse([]string{"--tristate=false"})
  119. if err != nil {
  120. t.Fatal("expected no error; got", err)
  121. }
  122. if tristate != triStateFalse {
  123. t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
  124. }
  125. }
  126. func TestImplicitFalse(t *testing.T) {
  127. var tristate triStateValue
  128. f := setUpFlagSet(&tristate)
  129. err := f.Parse([]string{})
  130. if err != nil {
  131. t.Fatal("expected no error; got", err)
  132. }
  133. if tristate != triStateFalse {
  134. t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead")
  135. }
  136. }
  137. func TestInvalidValue(t *testing.T) {
  138. var tristate triStateValue
  139. f := setUpFlagSet(&tristate)
  140. var buf bytes.Buffer
  141. f.SetOutput(&buf)
  142. err := f.Parse([]string{"--tristate=invalid"})
  143. if err == nil {
  144. t.Fatal("expected an error but did not get any, tristate has value", tristate)
  145. }
  146. }
  147. func TestBoolP(t *testing.T) {
  148. b := BoolP("bool", "b", false, "bool value in CommandLine")
  149. c := BoolP("c", "c", false, "other bool value")
  150. args := []string{"--bool"}
  151. if err := CommandLine.Parse(args); err != nil {
  152. t.Error("expected no error, got ", err)
  153. }
  154. if *b != true {
  155. t.Errorf("expected b=true got b=%v", *b)
  156. }
  157. if *c != false {
  158. t.Errorf("expect c=false got c=%v", *c)
  159. }
  160. }