-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (51 loc) · 941 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
cmds := getCliCommands()
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("Pokedex > ")
scanner.Scan()
method := scanner.Text()
if len(method) == 0 {
continue
}
method = cleanInput(method)
cmd, ok := cmds[method]
if !ok {
fmt.Println("Command not available")
continue
}
error := cmd.callback()
if error != nil {
fmt.Println(error)
}
}
}
type cliCommand struct {
name string
description string
callback func() error
}
func getCliCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
}
}
func cleanInput(text string) string {
return strings.TrimSpace(strings.ToLower(text))
}