Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | package flags |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | ) |
| 6 | |
| 7 | // ErrorType represents the type of error. |
| 8 | type ErrorType uint |
| 9 | |
| 10 | const ( |
| 11 | // ErrUnknown indicates a generic error. |
| 12 | ErrUnknown ErrorType = iota |
| 13 | |
| 14 | // ErrExpectedArgument indicates that an argument was expected. |
| 15 | ErrExpectedArgument |
| 16 | |
| 17 | // ErrUnknownFlag indicates an unknown flag. |
| 18 | ErrUnknownFlag |
| 19 | |
| 20 | // ErrUnknownGroup indicates an unknown group. |
| 21 | ErrUnknownGroup |
| 22 | |
| 23 | // ErrMarshal indicates a marshalling error while converting values. |
| 24 | ErrMarshal |
| 25 | |
| 26 | // ErrHelp indicates that the built-in help was shown (the error |
| 27 | // contains the help message). |
| 28 | ErrHelp |
| 29 | |
| 30 | // ErrNoArgumentForBool indicates that an argument was given for a |
| 31 | // boolean flag (which don't not take any arguments). |
| 32 | ErrNoArgumentForBool |
| 33 | |
| 34 | // ErrRequired indicates that a required flag was not provided. |
| 35 | ErrRequired |
| 36 | |
| 37 | // ErrShortNameTooLong indicates that a short flag name was specified, |
| 38 | // longer than one character. |
| 39 | ErrShortNameTooLong |
| 40 | |
| 41 | // ErrDuplicatedFlag indicates that a short or long flag has been |
| 42 | // defined more than once |
| 43 | ErrDuplicatedFlag |
| 44 | |
| 45 | // ErrTag indicates an error while parsing flag tags. |
| 46 | ErrTag |
| 47 | |
| 48 | // ErrCommandRequired indicates that a command was required but not |
| 49 | // specified |
| 50 | ErrCommandRequired |
| 51 | |
| 52 | // ErrUnknownCommand indicates that an unknown command was specified. |
| 53 | ErrUnknownCommand |
| 54 | |
| 55 | // ErrInvalidChoice indicates an invalid option value which only allows |
| 56 | // a certain number of choices. |
| 57 | ErrInvalidChoice |
| 58 | |
| 59 | // ErrInvalidTag indicates an invalid tag or invalid use of an existing tag |
| 60 | ErrInvalidTag |
| 61 | ) |
| 62 | |
| 63 | func (e ErrorType) String() string { |
| 64 | switch e { |
| 65 | case ErrUnknown: |
| 66 | return "unknown" |
| 67 | case ErrExpectedArgument: |
| 68 | return "expected argument" |
| 69 | case ErrUnknownFlag: |
| 70 | return "unknown flag" |
| 71 | case ErrUnknownGroup: |
| 72 | return "unknown group" |
| 73 | case ErrMarshal: |
| 74 | return "marshal" |
| 75 | case ErrHelp: |
| 76 | return "help" |
| 77 | case ErrNoArgumentForBool: |
| 78 | return "no argument for bool" |
| 79 | case ErrRequired: |
| 80 | return "required" |
| 81 | case ErrShortNameTooLong: |
| 82 | return "short name too long" |
| 83 | case ErrDuplicatedFlag: |
| 84 | return "duplicated flag" |
| 85 | case ErrTag: |
| 86 | return "tag" |
| 87 | case ErrCommandRequired: |
| 88 | return "command required" |
| 89 | case ErrUnknownCommand: |
| 90 | return "unknown command" |
| 91 | case ErrInvalidChoice: |
| 92 | return "invalid choice" |
| 93 | case ErrInvalidTag: |
| 94 | return "invalid tag" |
| 95 | } |
| 96 | |
| 97 | return "unrecognized error type" |
| 98 | } |
| 99 | |
| 100 | // Error represents a parser error. The error returned from Parse is of this |
| 101 | // type. The error contains both a Type and Message. |
| 102 | type Error struct { |
| 103 | // The type of error |
| 104 | Type ErrorType |
| 105 | |
| 106 | // The error message |
| 107 | Message string |
| 108 | } |
| 109 | |
| 110 | // Error returns the error's message |
| 111 | func (e *Error) Error() string { |
| 112 | return e.Message |
| 113 | } |
| 114 | |
| 115 | func newError(tp ErrorType, message string) *Error { |
| 116 | return &Error{ |
| 117 | Type: tp, |
| 118 | Message: message, |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func newErrorf(tp ErrorType, format string, args ...interface{}) *Error { |
| 123 | return newError(tp, fmt.Sprintf(format, args...)) |
| 124 | } |
| 125 | |
| 126 | func wrapError(err error) *Error { |
| 127 | ret, ok := err.(*Error) |
| 128 | |
| 129 | if !ok { |
| 130 | return newError(ErrUnknown, err.Error()) |
| 131 | } |
| 132 | |
| 133 | return ret |
| 134 | } |