-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.go
More file actions
32 lines (29 loc) · 772 Bytes
/
usage.go
File metadata and controls
32 lines (29 loc) · 772 Bytes
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
package cmdflag
import (
"flag"
"fmt"
"io"
)
// usage returns the default function used to display the help message.
func usage(out io.Writer, c *Command) func() {
return func() {
name := c.Application.Name
if c.Application.Init != nil {
// Not the program.
name = "command `" + name + "`"
}
_, _ = fmt.Fprintf(out, "Usage of %s:\n", name)
c.fset.PrintDefaults()
if cmds := c.Commands(); len(cmds) > 0 {
_, _ = fmt.Fprintf(out, "\nSubcommands:\n")
for _, c := range cmds {
app := c.Application
_, _ = fmt.Fprintf(out, "Usage of command `%s`:\n", app.Name)
_, _ = fmt.Fprintf(out, "%s\n%s %s\n", app.Descr, app.Name, app.Args)
fs := flag.NewFlagSet(app.Name, app.Err)
_ = app.Init(fs)
fs.PrintDefaults()
}
}
}
}