-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.go
More file actions
89 lines (84 loc) · 1.83 KB
/
usage.go
File metadata and controls
89 lines (84 loc) · 1.83 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
package cli
import (
"errors"
"fmt"
"strings"
"text/tabwriter"
)
func (cmd *Command) Usage(cc *Context, err error) {
if cmd.Hooks.Usage != nil {
cmd.Hooks.Usage(cc, err)
return
}
w := cc.Out
if err != nil {
w = cc.Err
}
fmt.Fprintf(w, "synopsis: %s\n", cmd.Synopsis)
if cmd.Description != "" {
fmt.Fprintln(w)
fmt.Fprintln(w, cmd.Description)
}
if len(cmd.Children) != 0 {
fmt.Fprintf(w, "\ncommands:\n")
tw := tabwriter.NewWriter(w, 1, 4, 2, ' ', 0)
for _, cmd := range cmd.Children {
fmt.Fprintf(tw, "\t")
if cmd.Synopsis != "" {
name, _, ok := strings.Cut(cmd.Synopsis, " ")
if ok {
fmt.Fprintf(tw, "\t%s\t%s", name, strings.TrimSpace(cmd.Synopsis))
} else {
fmt.Fprintf(tw, "\t%s\t", cmd.Name)
}
}
fmt.Fprintln(tw)
}
tw.Flush()
}
path := cmd.Path()
for i, c := range path {
available := "available "
if c == cmd {
available = ""
}
name := c.Name
if c.Parent == nil && len(path) == 1 {
name = ""
}
fmt.Fprintln(w)
if i != 0 {
fmt.Fprintln(w)
}
fmt.Fprintf(w, "%s%s options:", available, name)
if len(c.Opts) == 0 {
fmt.Fprintf(w, " (none)\n")
}
tw := tabwriter.NewWriter(w, 0, 1, 1, ' ', 0) //, tabwriter.TabIndent)
for _, co := range c.Opts {
fmt.Fprintf(tw, "\n\t%s\t\t%s", co.FormatFlag(), co.FormatDesc())
}
fmt.Fprintln(w)
tw.Flush()
}
if errors.Is(err, ErrUsage) {
fmt.Fprintln(w)
fmt.Fprintln(w)
fmt.Fprintln(w, err.Error())
}
}
func (o *Opt) FormatFlag() string {
b := &strings.Builder{}
b.WriteByte('-')
b.WriteString(o.Name)
for _, al := range o.Aliases {
b.WriteString(", -" + al)
}
return b.String()
}
func (o *Opt) FormatDesc() string {
if o.Default == nil {
return o.Description + "\t" + o.Type.String() + "\t"
}
return o.Description + fmt.Sprintf("\t(default %v)\t", *o.Default) + o.Type.String() + "\t"
}