-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.5 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
// name used in the repl prompts
var cliName string = "Pokedex"
// printPrompt displays the repl prompt at the start of each loop
func printPrompt() {
fmt.Print(strings.ToLower(cliName), "> ")
}
type cliCommand struct {
name string
description string
callback func() error
}
func commandHelp() error {
fmt.Printf("\nWelcome to the %v! \n\n", cliName)
fmt.Println("Usage:")
fmt.Println("help: Displays a help message")
fmt.Printf("exit: Exit the %v \n\n", cliName)
return errors.New("something didn't work")
}
func commandExit() error {
os.Exit(0)
return errors.New("something didn't work")
}
func main() {
/*
Starts up an interactive REPL when you run it.
It should print a prompt, wait for you to type in a command,
and then print the result of that command.
*/
commands := map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
}
// initialize input reader
reader := bufio.NewScanner(os.Stdin)
// print REPL prompt
printPrompt()
for reader.Scan() {
cmd := commands[strings.ToLower(reader.Text())]
cmd.callback()
// print REPL prompt
printPrompt()
}
}