bool.go 3.0 KB

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