-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (62 loc) · 1.78 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/RobolabGs2/botctl/cli"
"github.com/RobolabGs2/botctl/commands"
"github.com/RobolabGs2/flagconfig"
)
func main() {
cmds := map[string]cli.Command{
"duel": new(commands.DuelConfig),
"tournament": new(commands.Tournament),
"test": new(commands.TestBot),
}
commandName := "duel"
helpName := os.Args[0]
args := os.Args[1:]
if len(args) > 0 {
if _, ok := cmds[args[0]]; ok {
commandName = args[0]
args = args[1:]
helpName += " " + commandName
} else if args[0] == "help" {
writer := os.Stdout
if len(args) == 1 {
list := make([]string, 0, len(cmds))
for key := range cmds {
list = append(list, key)
}
sort.Strings(list)
fmt.Fprintln(writer, "Доступные команды:")
helpTable := tabwriter.NewWriter(writer, 0, 8, 8, ' ', 0)
for _, cmdName := range list {
cmd := cmds[cmdName]
_, _ = fmt.Fprintf(helpTable, "\t%s\t%s\n", cmdName, strings.Split(cmd.Description(), "\n")[0])
}
helpTable.Flush()
fmt.Fprintf(writer, "\nИспользуйте \"%s help <command>\" чтобы больше узнать о конкретной команде.\n",
helpName)
return
}
commandName = args[1]
helpName += " " + commandName
command := cmds[commandName]
flags, _ := flagconfig.MakeFlags(command, "", flag.ContinueOnError)
_, _ = fmt.Fprintf(writer, "использование:\n %s [flags] %s\n", helpName, command.Usage())
flags.SetOutput(writer)
flags.PrintDefaults()
_, _ = fmt.Fprintln(writer, "\n", command.Description())
return
}
}
err := cli.RunCommand(helpName, cmds[commandName], cli.StdStreams, args)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}