count.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package pflag
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. // -- count Value
  7. type countValue int
  8. func newCountValue(val int, p *int) *countValue {
  9. *p = val
  10. return (*countValue)(p)
  11. }
  12. func (i *countValue) Set(s string) error {
  13. v, err := strconv.ParseInt(s, 0, 64)
  14. // -1 means that no specific value was passed, so increment
  15. if v == -1 {
  16. *i = countValue(*i + 1)
  17. } else {
  18. *i = countValue(v)
  19. }
  20. return err
  21. }
  22. func (i *countValue) Type() string {
  23. return "count"
  24. }
  25. func (i *countValue) String() string { return fmt.Sprintf("%v", *i) }
  26. func countConv(sval string) (interface{}, error) {
  27. i, err := strconv.Atoi(sval)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return i, nil
  32. }
  33. // GetCount return the int value of a flag with the given name
  34. func (f *FlagSet) GetCount(name string) (int, error) {
  35. val, err := f.getFlagType(name, "count", countConv)
  36. if err != nil {
  37. return 0, err
  38. }
  39. return val.(int), nil
  40. }
  41. // CountVar defines a count flag with specified name, default value, and usage string.
  42. // The argument p points to an int variable in which to store the value of the flag.
  43. // A count flag will add 1 to its value evey time it is found on the command line
  44. func (f *FlagSet) CountVar(p *int, name string, usage string) {
  45. f.CountVarP(p, name, "", usage)
  46. }
  47. // CountVarP is like CountVar only take a shorthand for the flag name.
  48. func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
  49. flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
  50. flag.NoOptDefVal = "-1"
  51. }
  52. // CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set
  53. func CountVar(p *int, name string, usage string) {
  54. CommandLine.CountVar(p, name, usage)
  55. }
  56. // CountVarP is like CountVar only take a shorthand for the flag name.
  57. func CountVarP(p *int, name, shorthand string, usage string) {
  58. CommandLine.CountVarP(p, name, shorthand, usage)
  59. }
  60. // Count defines a count flag with specified name, default value, and usage string.
  61. // The return value is the address of an int variable that stores the value of the flag.
  62. // A count flag will add 1 to its value evey time it is found on the command line
  63. func (f *FlagSet) Count(name string, usage string) *int {
  64. p := new(int)
  65. f.CountVarP(p, name, "", usage)
  66. return p
  67. }
  68. // CountP is like Count only takes a shorthand for the flag name.
  69. func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
  70. p := new(int)
  71. f.CountVarP(p, name, shorthand, usage)
  72. return p
  73. }
  74. // Count like Count only the flag is placed on the CommandLine isntead of a given flag set
  75. func Count(name string, usage string) *int {
  76. return CommandLine.CountP(name, "", usage)
  77. }
  78. // CountP is like Count only takes a shorthand for the flag name.
  79. func CountP(name, shorthand string, usage string) *int {
  80. return CommandLine.CountP(name, shorthand, usage)
  81. }