49 lines
885 B
Go
49 lines
885 B
Go
package warren
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"text/template"
|
|
)
|
|
|
|
// shamelessly snagged from the go tool
|
|
// each command gets its own set of args,
|
|
// defines its own entry point, and provides its own help
|
|
type Command struct {
|
|
Run func(cmd *Command, args ...string)
|
|
Flag flag.FlagSet
|
|
|
|
Name string
|
|
Usage string
|
|
|
|
Summary string
|
|
Help string
|
|
}
|
|
|
|
var commands map[string]*Command = make(map[string]*Command)
|
|
|
|
func (c *Command) Exec(args []string) {
|
|
c.Flag.Usage = func() {
|
|
// helpFunc(c, c.Name)
|
|
}
|
|
c.Flag.Parse(args)
|
|
c.Run(c, c.Flag.Args()...)
|
|
}
|
|
|
|
func RegisterCommand(c *Command) {
|
|
commands[c.Name] = c
|
|
}
|
|
|
|
func GetCommand(name string) *Command {
|
|
return commands[name]
|
|
}
|
|
|
|
var usageTmpl = template.Must(template.New("usage").Parse(`
|
|
Commands:{{range .}}
|
|
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
|
|
`))
|
|
|
|
func PrintCommandUsage() {
|
|
usageTmpl.Execute(os.Stdout, commands)
|
|
}
|