pokedex_cli/repl.go

68 lines
1.2 KiB
Go
Raw Normal View History

2025-09-28 20:15:55 +00:00
package main
import (
"fmt"
"strings"
"bufio"
"os"
)
type cliCommand struct {
name string
description string
callback func() error
}
var supportedCommands map[string]cliCommand
func startRepl() {
reader := bufio.NewScanner(os.Stdin)
fmt.Println("Welcome to the Pokedex!")
for {
// Print prompt
fmt.Printf("Pokedex > ")
reader.Scan()
words := cleanInput(reader.Text())
if len(words) == 0 {
continue
}
commandName := words[0]
command, valid := getCommands()[commandName]
if !valid {
fmt.Println("Unknown command.")
continue
}
if err := command.callback(); err != nil {
fmt.Printf("Encountered error running command: %v\n", command.name)
}
}
}
func cleanInput(text string) []string {
if len(text) == 0 {
fmt.Println("Input is empty!")
return []string{}
}
output := strings.ToLower(text)
words := strings.Fields(output)
return words
}
func getCommands() map[string]cliCommand{
return map[string]cliCommand{
"help": {
name: "help",
description: "Prints this help menu.",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exits the Pokedex",
callback: commandExit,
},
}
}