-
Notifications
You must be signed in to change notification settings - Fork 3
/
help_cmd.go
55 lines (46 loc) · 1.09 KB
/
help_cmd.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
54
55
package main
import (
"flag"
"fmt"
)
// $kddp help prints some help information
type HelpCommand struct {
fs *flag.FlagSet // FlagSet for the arguments
cmd string // command to print information about, may be empty
}
func NewHelpCommand() *HelpCommand {
return &HelpCommand{
fs: flag.NewFlagSet("hilfe", flag.ExitOnError),
cmd: "",
}
}
func (cmd *HelpCommand) Init(args []string) error {
if err := parseFlagSet(cmd.fs, args); err != nil {
return err
}
cmd.cmd = cmd.fs.Arg(0)
return nil
}
func (cmd *HelpCommand) Run() error {
// if a sub-command was specified print only its usage
if cmd.cmd != "" {
for _, command := range commands {
if command.Name() == cmd.cmd {
fmt.Println(command.Usage())
return nil
}
}
}
// otherwise, print the usage of every command
fmt.Println("Verfügbare Befehle:")
for _, cmd := range commands {
fmt.Println(cmd.Usage() + "\n")
}
return nil
}
func (cmd *HelpCommand) Name() string {
return cmd.fs.Name()
}
func (cmd *HelpCommand) Usage() string {
return `hilfe <Befehl>: Zeigt Nutzungsinformationen über den Befehl`
}