本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0KB

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