-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.go
141 lines (112 loc) · 3.95 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package argparse
import (
"fmt"
"strings"
)
// InvalidChoiceErr indicates that an argument is not among the valid choices
// for the option.
type InvalidChoiceErr struct {
opt Option
arg string
}
// Error will return a string error message for the InvalidChoiceErr
func (err InvalidChoiceErr) Error() string {
msg := "%s: invalid choice \"%s\" (choose from: %s)"
return fmt.Sprintf(msg, err.opt.DisplayName(), err.arg, strings.Join(err.opt.ValidChoices, ", "))
}
// InvalidParserNameErr indicates that a Command name has already been assigned and cannot be re-assigned.
type InvalidParserNameErr struct {
name string
}
// Error will return a string error message for the InvalidParserNameErr
func (err InvalidParserNameErr) Error() string {
msg := "invalid command name \"%s\""
return fmt.Sprintf(msg, err.name)
}
// InvalidFlagNameErr indicates that an argument with the provided public name
// not exist.
type InvalidFlagNameErr struct {
name string
}
// Error will return a string error message for the InvalidFlagNameErr
func (err InvalidFlagNameErr) Error() string {
msg := "invalid flag name \"%s\""
return fmt.Sprintf(msg, err.name)
}
// InvalidOptionErr indicates that an option is invalid.
type InvalidOptionErr struct {
name string
}
// Error will return a string error message for the InvalidFlagNameErr
func (err InvalidOptionErr) Error() string {
msg := "invalid option \"%s\""
return fmt.Sprintf(msg, err.name)
}
// InvalidTypeErr indicates that an argument cannot be casted the the option's
// expected type.
type InvalidTypeErr struct {
opt Option
arg string
}
// Error will return a string error message for the InvalidTypeErr
func (err InvalidTypeErr) Error() string {
msg := "%s: invalid %s value: \"%s\""
return fmt.Sprintf(msg, err.opt.DisplayName(), err.opt.ExpectedType.String(), err.arg)
}
// MissingEnvVarErr indicates that an environmental variable could not be found
// with the provided variable name.
type MissingEnvVarErr struct {
varName string
}
// Error will return a string error message for the MissingEnvVarErr.
func (err MissingEnvVarErr) Error() string {
msg := "missing environmental variable \"%s\""
return fmt.Sprintf(msg, err.varName)
}
// ShowHelpErr indicates that the program was instructed to show it's help text.
type ShowHelpErr struct{}
func (err ShowHelpErr) Error() string { return "" }
// ShowVersionErr indicates that the program was instructed to show it's versioning text.
type ShowVersionErr struct{}
func (err ShowVersionErr) Error() string { return "" }
// TooFewArgsErr indicated that not enough arguments were provided for the option.
type TooFewArgsErr struct {
opt Option
}
// Error will return a string error message for the TooFewArgsErr
func (err TooFewArgsErr) Error() string {
msg := "%s: too few arguments"
return fmt.Sprintf(msg, err.opt.DisplayName())
}
// MissingOneOrMoreArgsErr indicated that not enough arguments were provided,
// when one or more arguments were expected, for the option.
type MissingOneOrMoreArgsErr struct {
opt Option
}
// Error will return a string error message for the TooFewArgsErr
func (err MissingOneOrMoreArgsErr) Error() string {
msg := "%s: at least one argument requireds"
return fmt.Sprintf(msg, err.opt.DisplayName())
}
// MissingParserErr indicated that commands were available, but none were used.
type MissingParserErr struct {
Parsers []SubParser
}
// Error will return a string error message for the MissingParserErr
func (err MissingParserErr) Error() string {
var names []string
for _, subP := range err.Parsers {
names = append(names, subP.Name)
}
msg := "must use an available command: %s"
return fmt.Sprintf(msg, join("", "{", join(",", names...), "}"))
}
// MissingOptionErr indicated that an option was required but is missing.
type MissingOptionErr struct {
name string
}
// Error will return a string error message for the MissingOptionErr
func (err MissingOptionErr) Error() string {
msg := "option \"%s\" required"
return fmt.Sprintf(msg, err.name)
}