-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (68 loc) · 1.25 KB
/
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
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
print("Hello Pokemon!")
scanner := bufio.NewScanner(os.Stdin)
prompt := "pokedex > "
for {
fmt.Print(prompt)
scanner.Scan()
words := cleanInput(scanner.Text())
if len(words) == 0 {
continue
}
cmdName := words[0]
cmd, exists := getCommands()[cmdName]
if exists {
err := cmd.callback()
if err != nil {
fmt.Println(err)
}
continue
} else {
fmt.Println("Unknown command")
continue
}
}
}
func cleanInput(text string) []string {
output := strings.ToLower(text)
words := strings.Fields(output)
return words
}
type cliCommand struct {
name string
description string
callback func() error
}
func commandHelp() error {
fmt.Println("Welcome to the Pokedex!")
fmt.Println("Usage:")
for _, cmd := range getCommands() {
fmt.Printf("%s: %s\n", cmd.name, cmd.description)
}
return nil
}
func commandExit() error {
os.Exit(0)
return nil
}
func getCommands() 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,
},
}
}