error.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package flags
  2. import (
  3. "fmt"
  4. )
  5. // ErrorType represents the type of error.
  6. type ErrorType uint
  7. const (
  8. // ErrUnknown indicates a generic error.
  9. ErrUnknown ErrorType = iota
  10. // ErrExpectedArgument indicates that an argument was expected.
  11. ErrExpectedArgument
  12. // ErrUnknownFlag indicates an unknown flag.
  13. ErrUnknownFlag
  14. // ErrUnknownGroup indicates an unknown group.
  15. ErrUnknownGroup
  16. // ErrMarshal indicates a marshalling error while converting values.
  17. ErrMarshal
  18. // ErrHelp indicates that the builtin help was shown (the error
  19. // contains the help message).
  20. ErrHelp
  21. // ErrNoArgumentForBool indicates that an argument was given for a
  22. // boolean flag (which don't not take any arguments).
  23. ErrNoArgumentForBool
  24. // ErrRequired indicates that a required flag was not provided.
  25. ErrRequired
  26. // ErrShortNameTooLong indicates that a short flag name was specified,
  27. // longer than one character.
  28. ErrShortNameTooLong
  29. // ErrDuplicatedFlag indicates that a short or long flag has been
  30. // defined more than once
  31. ErrDuplicatedFlag
  32. // ErrTag indicates an error while parsing flag tags.
  33. ErrTag
  34. )
  35. // String returns a string representation of the error type.
  36. func (e ErrorType) String() string {
  37. switch e {
  38. case ErrUnknown:
  39. return "unknown"
  40. case ErrExpectedArgument:
  41. return "expected argument"
  42. case ErrUnknownFlag:
  43. return "unknown flag"
  44. case ErrUnknownGroup:
  45. return "unknown group"
  46. case ErrMarshal:
  47. return "marshal"
  48. case ErrHelp:
  49. return "help"
  50. case ErrNoArgumentForBool:
  51. return "no argument for bool"
  52. case ErrRequired:
  53. return "required"
  54. case ErrShortNameTooLong:
  55. return "short name too long"
  56. case ErrDuplicatedFlag:
  57. return "duplicated flag"
  58. case ErrTag:
  59. return "tag"
  60. }
  61. return "unknown"
  62. }
  63. // Error represents a parser error. The error returned from Parse is of this
  64. // type. The error contains both a Type and Message.
  65. type Error struct {
  66. // The type of error
  67. Type ErrorType
  68. // The error message
  69. Message string
  70. }
  71. // Error returns the error's message
  72. func (e *Error) Error() string {
  73. return e.Message
  74. }
  75. func newError(tp ErrorType, message string) *Error {
  76. return &Error{
  77. Type: tp,
  78. Message: message,
  79. }
  80. }
  81. func newErrorf(tp ErrorType, format string, args ...interface{}) *Error {
  82. return newError(tp, fmt.Sprintf(format, args...))
  83. }
  84. func wrapError(err error) *Error {
  85. ret, ok := err.(*Error)
  86. if !ok {
  87. return newError(ErrUnknown, err.Error())
  88. }
  89. return ret
  90. }