-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
43 lines (38 loc) · 978 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
package main
import (
"flag"
"fmt"
"github.com/chzyer/readline"
"github.com/muesli/termenv"
"os"
)
func HelpCmd(args []string) {
flagSet := flag.NewFlagSet("help", flag.ContinueOnError)
flagSet.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage of help:\n help <cmd>")
flagSet.PrintDefaults()
}
err := flagSet.Parse(args)
if err == flag.ErrHelp {
return
} else if err != nil {
fmt.Fprintln(os.Stderr, termenv.String(err.Error()).Foreground(termenv.ANSIRed))
return
}
if flagSet.NArg() == 0 {
flagSet.Usage()
return
}
if flagSet.NArg() > 1 {
fmt.Fprintln(os.Stderr, termenv.String("ls need at most one argument").Foreground(termenv.ANSIRed))
return
}
Dispatch([]string{flagSet.Arg(0), "-help"})
}
func HelpCompleter(cmds ...string) *readline.PrefixCompleter {
cmdTexts := []readline.PrefixCompleterInterface{}
for _, cmd := range cmds {
cmdTexts = append(cmdTexts, readline.PcItem(cmd))
}
return readline.PcItem("help", cmdTexts...)
}