github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/control/command.go (about) 1 package control 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "strings" 8 ) 9 10 type Description struct { 11 Short string 12 Usage []string 13 } 14 15 type Command interface { 16 Name() string 17 Description() Description 18 Execute(args []string) error 19 } 20 21 var ( 22 commandRegistry = make(map[string]Command) 23 ctllog = log.New(os.Stderr, "v2ctl> ", 0) 24 ) 25 26 func RegisterCommand(cmd Command) error { 27 entry := strings.ToLower(cmd.Name()) 28 if entry == "" { 29 return newError("empty command name") 30 } 31 commandRegistry[entry] = cmd 32 return nil 33 } 34 35 func GetCommand(name string) Command { 36 cmd, found := commandRegistry[name] 37 if !found { 38 return nil 39 } 40 return cmd 41 } 42 43 type hiddenCommand interface { 44 Hidden() bool 45 } 46 47 func PrintUsage() { 48 for name, cmd := range commandRegistry { 49 if _, ok := cmd.(hiddenCommand); ok { 50 continue 51 } 52 fmt.Println(" ", name, "\t\t\t", cmd.Description()) 53 } 54 }