-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubcmds.go
118 lines (101 loc) · 2.84 KB
/
subcmds.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2014 Rocky Bernstein.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// REPL subcommands
package repl
import (
"sort"
"strings"
"code.google.com/p/go-columnize"
)
type SubcmdFunc func([]string)
type SubcmdInfo struct {
Help string
Short_help string
Min_args int
Max_args int
Fn SubcmdFunc
Name string
}
type SubcmdMap map[string]*SubcmdInfo
// SubcmdMgr contains the name of a debugger command that has
// subcommands, "info", and "set" are examples of such commmands,and
// information about each of the subcommands.
type SubcmdMgr struct {
Name string
Subcmds SubcmdMap
}
// Subcmds is a map of a debugger subcommand name to information about
// that subcommand. For example, the "info" command has subcommands
// "break", "program", "line" and so on.
var Subcmds map[string]*SubcmdInfo = make(map[string]*SubcmdInfo)
// AddSubCommand adds to the subcommand manager mgrName, subcommand
// subcmdInfo.
func AddSubCommand(mgrName string, subcmdInfo *SubcmdInfo) {
Subcmds[mgrName] = subcmdInfo
mgr := Cmds[mgrName]
if mgr != nil {
mgr.SubcmdMgr.Subcmds[subcmdInfo.Name] = subcmdInfo
} else {
Errmsg("Internal error: can't find command '%s' to add to", subcmdInfo.Name)
}
}
func ListSubCommandArgs(mgr *SubcmdMgr) {
Section("List of " + mgr.Name + " commands")
subcmds := mgr.Subcmds
names := make([]string, len(subcmds))
i := 0
for name, _ := range subcmds {
names[i] = name
i++
}
sort.Strings(names)
for _, name := range names {
Msg("%-10s -- %s", name, subcmds[name].Short_help)
}
}
func HelpSubCommand(subcmdMgr *SubcmdMgr, args []string) {
if len(args) == 2 {
Msg(Cmds[subcmdMgr.Name].Help)
} else {
what := args[2]
if what == "*" {
var names []string
for name, _ := range subcmdMgr.Subcmds {
names = append(names, name)
}
Section("All %s subcommand names:", subcmdMgr.Name)
sort.Strings(names)
opts := columnize.DefaultOptions()
opts.DisplayWidth = Maxwidth
mems := strings.TrimRight(columnize.Columnize(names, opts),
"\n")
Msg(mems)
} else if info := subcmdMgr.Subcmds[what]; info != nil {
Msg(info.Help)
} else {
Errmsg("Can't find help for subcommand '%s' in %s", what, subcmdMgr.Name)
}
}
}
func UnknownSubCommand(cmdName, subcmdName string) {
Errmsg("Unknown \"%st\" subcommand \"%s\".", cmdName, subcmdName)
Errmsg("Try \"help %s *\".", cmdName)
}
func SubcmdMgrCommand(args []string) {
cmdName := args[0]
if len(args) == 1 {
ListSubCommandArgs(Cmds[cmdName].SubcmdMgr)
return
}
subcmd_name := args[1]
subcmds := Cmds[cmdName].SubcmdMgr.Subcmds
subcmd_info := subcmds[subcmd_name]
if subcmd_info != nil {
if ArgCountOK(subcmd_info.Min_args+1, subcmd_info.Max_args+1, args) {
subcmds[subcmd_name].Fn(args)
}
return
}
Errmsg("Unknown \"%s\" subcommand \"%s\"", cmdName, subcmd_name)
}