-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (59 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
package main
import (
dic "dictionary/dictionary"
"flag"
"fmt"
"os"
)
func main() {
action := flag.String("action", "list", "action to perform n dectionary")
d, err := dic.New("./badger")
handleErr(err)
defer d.Close()
flag.Parse()
switch *action {
case "list":
actionList(d)
case "add":
actionAdd(d, flag.Args())
case "define":
actionDefine(d, flag.Args())
case "remove":
actionRemove(d, flag.Args())
default:
fmt.Printf("unknown action : %v\n", *action)
}
d.Add("php", "is a good langage")
}
func actionRemove(d *dic.Dictionary, args []string) {
word := args[0]
err := d.Remove(word)
handleErr(err)
fmt.Printf("'%v' was removed from the dictionary\n", word)
}
func actionDefine(d *dic.Dictionary, args []string) {
word := args[0]
entry, err := d.Get(word)
handleErr(err)
fmt.Println(entry)
}
func actionAdd(d *dic.Dictionary, args []string) {
word := args[0]
defintion := args[1]
err := d.Add(word, defintion)
handleErr(err)
fmt.Printf("%v added to dictionary\n", word)
}
func actionList(d *dic.Dictionary) {
words, entries, err := d.List()
handleErr(err)
for _, word := range words {
fmt.Println(entries[word])
}
}
func handleErr(err error) {
if err != nil {
fmt.Printf("Dictionary error: %v\n", err)
os.Exit(1)
}
}