-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
68 lines (55 loc) · 1.88 KB
/
Copy patherrors.go
File metadata and controls
68 lines (55 loc) · 1.88 KB
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
package opts
import (
"errors"
"fmt"
"strconv"
"strings"
)
// ErrAlreadyParsed signals an attempt to parse a [Group] that has already been
// successfully parsed.
var ErrAlreadyParsed = errors.New("already parsed")
// ErrBooleanWithValue signals an unsupported use of "option=value" with
// a boolean.
var ErrBooleanWithValue = errors.New("boolean options do not accept values")
// ErrMissingValue signals that an option is missing a required value.
var ErrMissingValue = errors.New("missing required value")
// ErrUnknownOption signals that an option was not registered with the [Group].
var ErrUnknownOption = errors.New("unknown option")
// UnexpectedArgumentsError signals that there are one or more arguments left
// after parsing. Only [Parse] will return this error. Use [ParseKnown] for
// relaxed parsing.
type UnexpectedArgumentsError struct {
Args []string
}
func (e *UnexpectedArgumentsError) Error() string {
var s string
if len(e.Args) > 1 {
s = "s"
}
return fmt.Sprintf("opts: unexpected argument%s: %s", s, quotedArgs(e.Args))
}
// We can rely on Args having at least one item since Parse does not return
// UnexpectedArgumentsError if there are no leftover arguments.
func quotedArgs(items []string) string {
var b strings.Builder
b.WriteString(strconv.Quote(items[0]))
for _, str := range items[1:] {
b.WriteString(", ")
b.WriteString(strconv.Quote(str))
}
return b.String()
}
// InvalidValueError signals that an option's value cannot be converted into
// the option's type. Since InvalidValueError wraps the original conversion
// error, users can access the undedited original as InvalidValueError.Err.
type InvalidValueError struct {
Err error
Option string
Value string
}
func (e *InvalidValueError) Error() string {
return fmt.Sprintf("opts: invalid value %q for --%s: %v", e.Value, e.Option, e.Err)
}
func (e *InvalidValueError) Unwrap() error {
return e.Err
}