本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

85 lines
2.7KB

  1. package pflag
  2. import "strconv"
  3. // -- int Value
  4. type intValue int
  5. func newIntValue(val int, p *int) *intValue {
  6. *p = val
  7. return (*intValue)(p)
  8. }
  9. func (i *intValue) Set(s string) error {
  10. v, err := strconv.ParseInt(s, 0, 64)
  11. *i = intValue(v)
  12. return err
  13. }
  14. func (i *intValue) Type() string {
  15. return "int"
  16. }
  17. func (i *intValue) String() string { return strconv.Itoa(int(*i)) }
  18. func intConv(sval string) (interface{}, error) {
  19. return strconv.Atoi(sval)
  20. }
  21. // GetInt return the int value of a flag with the given name
  22. func (f *FlagSet) GetInt(name string) (int, error) {
  23. val, err := f.getFlagType(name, "int", intConv)
  24. if err != nil {
  25. return 0, err
  26. }
  27. return val.(int), nil
  28. }
  29. // IntVar defines an int flag with specified name, default value, and usage string.
  30. // The argument p points to an int variable in which to store the value of the flag.
  31. func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
  32. f.VarP(newIntValue(value, p), name, "", usage)
  33. }
  34. // IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.
  35. func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string) {
  36. f.VarP(newIntValue(value, p), name, shorthand, usage)
  37. }
  38. // IntVar defines an int 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. func IntVar(p *int, name string, value int, usage string) {
  41. CommandLine.VarP(newIntValue(value, p), name, "", usage)
  42. }
  43. // IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.
  44. func IntVarP(p *int, name, shorthand string, value int, usage string) {
  45. CommandLine.VarP(newIntValue(value, p), name, shorthand, usage)
  46. }
  47. // Int defines an int flag with specified name, default value, and usage string.
  48. // The return value is the address of an int variable that stores the value of the flag.
  49. func (f *FlagSet) Int(name string, value int, usage string) *int {
  50. p := new(int)
  51. f.IntVarP(p, name, "", value, usage)
  52. return p
  53. }
  54. // IntP is like Int, but accepts a shorthand letter that can be used after a single dash.
  55. func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int {
  56. p := new(int)
  57. f.IntVarP(p, name, shorthand, value, usage)
  58. return p
  59. }
  60. // Int defines an int 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. func Int(name string, value int, usage string) *int {
  63. return CommandLine.IntP(name, "", value, usage)
  64. }
  65. // IntP is like Int, but accepts a shorthand letter that can be used after a single dash.
  66. func IntP(name, shorthand string, value int, usage string) *int {
  67. return CommandLine.IntP(name, shorthand, value, usage)
  68. }
上海开阖软件有限公司 沪ICP备12045867号-1