refactor command line code
This commit is contained in:
@ -2,13 +2,14 @@ package lumecmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.kill0.net/chill9/go-lifx"
|
||||
)
|
||||
|
||||
type CmdArgs struct {
|
||||
Flags *Flags
|
||||
Flags Flags
|
||||
Client *lifx.Client
|
||||
Selector string
|
||||
}
|
||||
@ -17,6 +18,13 @@ type Flags struct {
|
||||
*flag.FlagSet
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Func func(CmdArgs) int
|
||||
Flags *flag.FlagSet
|
||||
}
|
||||
|
||||
var commandRegistry = make(map[string]Command)
|
||||
|
||||
func (f Flags) String(name string) string {
|
||||
return f.FlagSet.Lookup(name).Value.String()
|
||||
}
|
||||
@ -30,3 +38,16 @@ func (f Flags) Bool(name string) bool {
|
||||
val, _ := strconv.ParseBool(f.String(name))
|
||||
return val
|
||||
}
|
||||
|
||||
func RegisterCommand(name string, cmd Command) error {
|
||||
if _, ok := commandRegistry[name]; ok {
|
||||
return fmt.Errorf("%s command is already registered")
|
||||
}
|
||||
commandRegistry[name] = cmd
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetCommand(name string) (Command, bool) {
|
||||
cmd, ok := commandRegistry[name]
|
||||
return cmd, ok
|
||||
}
|
||||
|
Reference in New Issue
Block a user