1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/infra/control/command.go

52 lines
797 B
Go
Raw Normal View History

2019-02-10 13:04:11 -05:00
package control
import (
"fmt"
"strings"
)
type Description struct {
Short string
Usage []string
}
type Command interface {
Name() string
Description() Description
Execute(args []string) error
}
var (
commandRegistry = make(map[string]Command)
)
func RegisterCommand(cmd Command) error {
entry := strings.ToLower(cmd.Name())
if len(entry) == 0 {
return newError("empty command name")
}
commandRegistry[entry] = cmd
return nil
}
func GetCommand(name string) Command {
cmd, found := commandRegistry[name]
if !found {
return nil
}
return cmd
}
type hiddenCommand interface {
Hidden() bool
}
func PrintUsage() {
for name, cmd := range commandRegistry {
if _, ok := cmd.(hiddenCommand); ok {
continue
}
fmt.Println(" ", name, "\t\t\t", cmd.Description())
}
}