-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble.go
More file actions
48 lines (43 loc) · 944 Bytes
/
ensemble.go
File metadata and controls
48 lines (43 loc) · 944 Bytes
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
package gotcl
import (
"fmt"
"sort"
"strings"
)
func formatNames(sv []string) string {
if len(sv) == 0 {
return ""
}
if len(sv) == 1 {
return sv[0]
}
sort.Strings(sv)
return strings.Join(sv[0:len(sv)-1], ", ") + ", or " + sv[len(sv)-1]
}
type ensembleSpec map[string]interface{}
func (es ensembleSpec) makeCmd() TclCmd {
cmds := make(map[string]TclCmd, len(es))
for k, v := range es {
cmds[k] = MakeCmd(v)
}
return func(i *Interp, args []*TclObj) TclStatus {
if len(args) == 0 {
return i.FailStr("wrong # args")
}
return doEnsemble(cmds, args[0].AsString(), i, args[1:])
}
}
func doEnsemble(e map[string]TclCmd, cmd string, i *Interp, args []*TclObj) TclStatus {
c, ok := e[cmd]
if ok {
return c(i, args)
}
sv := make([]string, len(e))
ind := 0
for k := range e {
sv[ind] = k
ind++
}
return i.FailStr(
fmt.Sprintf("unknown or ambiguous subcommand \"%s\". Must be %s.", cmd, formatNames(sv)))
}