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.

134 lines
2.9 KiB

  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 built-in 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. // ErrCommandRequired indicates that a command was required but not
  35. // specified
  36. ErrCommandRequired
  37. // ErrUnknownCommand indicates that an unknown command was specified.
  38. ErrUnknownCommand
  39. // ErrInvalidChoice indicates an invalid option value which only allows
  40. // a certain number of choices.
  41. ErrInvalidChoice
  42. // ErrInvalidTag indicates an invalid tag or invalid use of an existing tag
  43. ErrInvalidTag
  44. )
  45. func (e ErrorType) String() string {
  46. switch e {
  47. case ErrUnknown:
  48. return "unknown"
  49. case ErrExpectedArgument:
  50. return "expected argument"
  51. case ErrUnknownFlag:
  52. return "unknown flag"
  53. case ErrUnknownGroup:
  54. return "unknown group"
  55. case ErrMarshal:
  56. return "marshal"
  57. case ErrHelp:
  58. return "help"
  59. case ErrNoArgumentForBool:
  60. return "no argument for bool"
  61. case ErrRequired:
  62. return "required"
  63. case ErrShortNameTooLong:
  64. return "short name too long"
  65. case ErrDuplicatedFlag:
  66. return "duplicated flag"
  67. case ErrTag:
  68. return "tag"
  69. case ErrCommandRequired:
  70. return "command required"
  71. case ErrUnknownCommand:
  72. return "unknown command"
  73. case ErrInvalidChoice:
  74. return "invalid choice"
  75. case ErrInvalidTag:
  76. return "invalid tag"
  77. }
  78. return "unrecognized error type"
  79. }
  80. // Error represents a parser error. The error returned from Parse is of this
  81. // type. The error contains both a Type and Message.
  82. type Error struct {
  83. // The type of error
  84. Type ErrorType
  85. // The error message
  86. Message string
  87. }
  88. // Error returns the error's message
  89. func (e *Error) Error() string {
  90. return e.Message
  91. }
  92. func newError(tp ErrorType, message string) *Error {
  93. return &Error{
  94. Type: tp,
  95. Message: message,
  96. }
  97. }
  98. func newErrorf(tp ErrorType, format string, args ...interface{}) *Error {
  99. return newError(tp, fmt.Sprintf(format, args...))
  100. }
  101. func wrapError(err error) *Error {
  102. ret, ok := err.(*Error)
  103. if !ok {
  104. return newError(ErrUnknown, err.Error())
  105. }
  106. return ret
  107. }