-
Notifications
You must be signed in to change notification settings - Fork 3
/
help.go
53 lines (42 loc) · 843 Bytes
/
help.go
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
package leetgode
import (
"context"
"fmt"
"io"
"sort"
"text/tabwriter"
)
var _ Cmd = &HelpCmd{}
type HelpCmd struct{}
func (c *HelpCmd) Name() string {
return "help"
}
func (c *HelpCmd) Usage() string {
return "Help shows usages"
}
func (c *HelpCmd) MaxArg() int {
return 0
}
var desc = `
Usage: leetgode is leetcode cli for gophers.
SubCommands:
`
func ShowUsage(w io.Writer) error {
cms := make([]string, len(CmdMap))
i := 0
for k := range CmdMap {
cms[i] = string(k)
i++
}
sort.Strings(cms)
tw := tabwriter.NewWriter(w, 0, 4, 1, ' ', 0)
fmt.Fprintf(tw, "%s", desc)
for _, k := range cms {
cn := CmdName(k)
fmt.Fprintf(tw, "\t%s\t%s\n", CmdMap[cn].Name(), CmdMap[cn].Usage())
}
return tw.Flush()
}
func (c *HelpCmd) Run(ctx context.Context, out io.Writer, args []string) error {
return ShowUsage(out)
}