-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.go
More file actions
83 lines (76 loc) · 1.86 KB
/
usage.go
File metadata and controls
83 lines (76 loc) · 1.86 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
package cli
import (
"errors"
"io"
"io/fs"
)
// nilUsage represents the nil usage.
type nilUsage struct{}
// Open implements the io/fs.FS interface.
func (u *nilUsage) Open(name string) (fs.File, error) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
// UsageFS is a io/fs.FS implementation that
// reads files from usage lookup keys.
type UsageFS struct {
fs fs.FS
ext string
index string
}
// NewUsageFS returns a usage lookup fs.FS implementation.
func NewUsageFS(fs fs.FS, opts ...UsageOption) fs.FS {
if fs == nil {
return &nilUsage{}
}
u := &UsageFS{fs: fs}
for _, option := range opts {
option(u)
}
if u.ext == "" {
u.ext = ".md"
}
if u.index == "" {
u.index = "README"
}
return u
}
// Open implements the io/fs.FS interface.
func (u *UsageFS) Open(name string) (fs.File, error) {
if name == "" || name[len(name)-1] == '/' {
name += u.index + u.ext
} else {
name += u.ext
}
return u.fs.Open(name)
}
// Usage displays the application usage information.
//
// The usage FS will be called with the help topic. The
// help topic is prefixed with the configured scope if the
// topic is a registered command. For example, if the scope
// is "cli" and the "foo" command is registered, "help foo"
// will call the renderer with "cli/foo" but "help not-found"
// would passthrough as "not-found" without the scope.
func (c *CLI) Usage(w io.Writer, name string) error {
key := name
_, ok := c.commands[name]
if ok {
key = c.scope + name
}
b, err := fs.ReadFile(c.usage, key)
if err != nil {
var perr *fs.PathError
if !errors.As(err, &perr) {
return err
}
if name == "" || name == c.scope {
c.Errorf("Unknown help topic.\n")
} else {
c.Errorf("Unknown help topic '%s'.\n", name)
}
c.Errorf("Run '%s help' for usage information.\n", c.name)
return ErrExitFailure
}
_, err = w.Write(b)
return err
}