-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
94 lines (78 loc) · 2.68 KB
/
Copy patherrors.go
File metadata and controls
94 lines (78 loc) · 2.68 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
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
package mflags
import (
"fmt"
"strings"
)
// UnknownCommandError reports a command word that matches nothing registered.
// It carries the surrounding context — which program, which parent command, and
// what the user might have meant — so callers can render it themselves rather
// than parsing the message back out of a string.
type UnknownCommandError struct {
// Program is the binary name, e.g. "miren".
Program string
// ParentPath is the command the unknown word appeared under, e.g. "app".
// Empty when the word was typed at the top level.
ParentPath string
// Name is the single word that matched nothing.
Name string
// Suggestions holds close matches, nearest first. May be empty.
Suggestions []string
}
func (e *UnknownCommandError) Error() string {
helpTarget := strings.TrimSpace(e.Program + " " + e.ParentPath)
var sections []string
if e.ParentPath != "" {
sections = append(sections, fmt.Sprintf("unknown command %q for %q", e.Name, helpTarget))
} else {
sections = append(sections, fmt.Sprintf("unknown command %q", e.Name))
}
if len(e.Suggestions) > 0 {
var b strings.Builder
b.WriteString("Did you mean?")
for _, s := range e.Suggestions {
fmt.Fprintf(&b, "\n %s", s)
}
sections = append(sections, b.String())
}
if helpTarget != "" {
sections = append(sections, fmt.Sprintf("Run '%s --help' to see available commands.", helpTarget))
}
return strings.Join(sections, "\n\n")
}
// UnknownFlagError reports a flag the command does not define. It unwraps to
// ErrUnknownFlag, so existing errors.Is checks keep working.
type UnknownFlagError struct {
// Flag is the flag as the user typed it, dashes included.
Flag string
// Suggestions holds close long-flag names, dashes included, nearest first.
// Short flags carry no suggestions: a single letter is too little to guess
// from.
Suggestions []string
}
func (e *UnknownFlagError) Error() string {
sections := []string{"unknown flag: " + e.Flag}
if len(e.Suggestions) > 0 {
var b strings.Builder
b.WriteString("Did you mean?")
for _, s := range e.Suggestions {
fmt.Fprintf(&b, "\n %s", s)
}
sections = append(sections, b.String())
}
return strings.Join(sections, "\n\n")
}
func (e *UnknownFlagError) Unwrap() error { return ErrUnknownFlag }
// UnexpectedArgsError reports positional arguments a command has nowhere to put.
type UnexpectedArgsError struct {
Args []string
}
func (e *UnexpectedArgsError) Error() string {
if len(e.Args) == 1 {
return fmt.Sprintf("unexpected argument %q", e.Args[0])
}
quoted := make([]string, 0, len(e.Args))
for _, a := range e.Args {
quoted = append(quoted, fmt.Sprintf("%q", a))
}
return "unexpected arguments: " + strings.Join(quoted, " ")
}