count.go 2.9 KB

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