blob: 73e07cfc2968364393548ef40409f09731fb0cb6 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301package flags
2
3import (
4 "fmt"
5)
6
7// ErrorType represents the type of error.
8type ErrorType uint
9
10const (
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
63func (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
100func (e ErrorType) Error() string {
101 return e.String()
102}
103
104// Error represents a parser error. The error returned from Parse is of this
105// type. The error contains both a Type and Message.
106type Error struct {
107 // The type of error
108 Type ErrorType
109
110 // The error message
111 Message string
112}
113
114// Error returns the error's message
115func (e *Error) Error() string {
116 return e.Message
117}
118
119func newError(tp ErrorType, message string) *Error {
120 return &Error{
121 Type: tp,
122 Message: message,
123 }
124}
125
126func newErrorf(tp ErrorType, format string, args ...interface{}) *Error {
127 return newError(tp, fmt.Sprintf(format, args...))
128}
129
130func wrapError(err error) *Error {
131 ret, ok := err.(*Error)
132
133 if !ok {
134 return newError(ErrUnknown, err.Error())
135 }
136
137 return ret
138}