Documentation
¶
Overview ¶
Package cmdmux is used to parse and route commands of terminal program.
Simple Example, used like package `http`:
package main
import (
"fmt"
"os"
"github.com/choueric/cmdmux"
)
type Options struct {
arch string
}
func rootHandler(args []string, data interface{}) (int, error) {
fmt.Println("Usage:")
cmdmux.PrintTree(os.Stderr)
return 0, nil
}
func buildHandler(args []string, data interface{}) (int, error) {
opt := data.(*Options)
fmt.Printf("invoke 'build' of %s\n", opt.arch)
return 1, nil
}
func buildKernelHandler(args []string, data interface{}) (int, error) {
fmt.Printf("invoke 'build kernel', args = %v\n", args)
return 2, nil
}
func main() {
opt := &Options{arch: "arm"}
cmdmux.HandleFunc("/", rootHandler)
cmdmux.HandleFunc("/build", buildHandler)
cmdmux.HandleFunc("/build/kernel", buildKernelHandler)
cmdmux.HandleFunc("/build/kernel/image", buildKernelHandler)
cmdmux.HandleFunc("/build/uboot", buildKernelHandler)
cmdmux.Execute(opt)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Execute ¶
Execute accepts the os.Args as command and executes it with data in the default CmdMux
func GenerateCompletion ¶
GenerateCompletion generates a *bash* completion file with the program name.
func HandleFunc ¶
func HandleFunc(cmdpath string, handler CmdHandler) error
HandleFunc registers the handler function for the given command path cmdpath in the default CmdMux.
Types ¶
type CmdHandler ¶
CmdHanlder is the type of callback function for command. if error is nil, then return value int is useful.
type CmdMux ¶
type CmdMux struct {
// contains filtered or unexported fields
}
CmdMux represents program's commands.
func (*CmdMux) GenerateCompletion ¶
GenerateCompletion generates a *bash* completion file with the program name.
func (*CmdMux) HandleFunc ¶
func (c *CmdMux) HandleFunc(cmdpath string, handler CmdHandler) error
HandleFunc registers the handler function for the given command path cmdpath