-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepl.go
99 lines (88 loc) · 1.89 KB
/
repl.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func startRepl(cfg *config) {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print(" >")
scanner.Scan()
text := scanner.Text()
cleaned := cleanInput(text)
if len(cleaned) == 0 {
continue
}
command := cleaned[0]
args := []string{}
if len(cleaned) > 1 {
args = cleaned[1:]
}
availableCommands := getCommands()
commandName, ok := availableCommands[command]
if !ok {
fmt.Println("Invalid Command!")
continue
}
err := commandName.callback(cfg, args...)
if err != nil {
fmt.Println(err)
}
}
}
type cliCommand struct {
name string
description string
callback func(*config, ...string) error
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "prints the help menu",
callback: callbackHelp,
},
"map": {
name: "map",
description: "List some location ares",
callback: callbackMap,
},
"mapb": {
name: "mapb",
description: "List some location ares (map backwards)",
callback: callbackMapb,
},
"explore": {
name: "explore",
description: "List all pokemon in a area",
callback: callbackExpore,
},
"catch": {
name: "catch {pokemon name}",
description: "Attempt to catch pokemon and add it to our pokedex ",
callback: callbackCatch,
},
"exit": {
name: "exit",
description: "exists from the cli",
callback: callbackExit,
},
"pokedex": {
name: "pokedex",
description: "View all pokemon in pokedex",
callback: callbackPokedex,
},
"inspect": {
name: "inspect {pokemon-name}",
description: "Inspects the caught pokemon stats",
callback: callbackInspect,
},
}
}
func cleanInput(str string) []string {
lowStr := strings.ToLower(str)
words := strings.Fields(lowStr)
return words
}