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{"login", "l"}, Description: "Log into the system", Handler: func(args []string) { fmt.Println("Executing login with args:", args) }, }, { Names: []string{"auth", "authenticate"}, Description: "Authenticate with a token", Handler: authenticateMain, }, } 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) *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) if cmd == nil { fmt.Println("Error: unknown command:", subcommand) printMainUsage() os.Exit(1) } cmd.Handler(os.Args[2:]) }