package main import ( "fmt" "os" "strings" ) var endpoint string = "http://127.0.0.1:8080" type Command struct { Names []string // aliases: short + long Description string Handler func(args []string) } var commands = []Command{ { Names: []string{"role"}, Description: "Role Management", Handler: roleMain, }, { Names: []string{"auth", "authorization"}, Description: "Role Management", Handler: authenticateMain, }, { Names: []string{"policy"}, Description: "Policy Management", Handler: policyMain, }, } func printMainUsage() { fmt.Println("Usage:") fmt.Println(" upc [options]") fmt.Println("\nAvailable commands:") for _, cmd := range commands { fmt.Printf(" %-20s %s\n", formatNames(cmd.Names), cmd.Description) } } func formatNames(names []string) string { return strings.Join(names, ", ") } func findCommand(name string, commands []Command) *Command { for _, cmd := range commands { for _, n := range cmd.Names { if n == name { return &cmd } } } return nil } func main() { if len(os.Args) < 2 { fmt.Println("Error: missing subcommand") printMainUsage() os.Exit(1) } subcommand := os.Args[1] cmd := findCommand(subcommand, commands) if cmd == nil { fmt.Println("Error: unknown command:", subcommand) printMainUsage() os.Exit(1) } cmd.Handler(os.Args[2:]) }