-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (79 loc) · 1.79 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
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"fmt"
"os"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n%s [opts] [roll1] [roll2] [roll3] [...]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
}
cli := cliFlags{}
setupFlags(&cli)
flag.Parse()
if cli.printVersion {
printVersion(cli.beVerbose)
return
}
if cli.listLists {
for _, name := range internalListNames() {
fmt.Println(name)
}
return
}
if cli.dumpList {
if cli.wordList == "" {
fmt.Fprintf(os.Stderr, "error: missing -list\n")
return
}
doDumpList(cli.wordList, cli.printHorizontal, cli.beVerbose)
return
}
lines, err := getWordLines(cli.wordFile, cli.wordList)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(2)
}
printer := &wordPrinter{
os.Stdout,
cli.printUnpadded, cli.wordSep,
!cli.printHorizontal,
cli.beVerbose,
}
if cli.rollElectronic {
doRollOnList(cli.nRolls, lines, cli.extra, printer)
return
}
if len(flag.Args()) == 0 {
fmt.Println("no rolls given, no lookup possible.")
flag.Usage()
return
}
if err := doLookupRolls(flag.Args(), lines, printer); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
func getWordLines(filename string, listName string) ([]string, error) {
if filename != "" {
return getWordLinesFromFile(filename)
}
return getWordLinesFromInternal(listName)
}
func getWordLinesFromFile(name string) ([]string, error) {
file, err := os.Open(name)
if err != nil {
return []string{}, fmt.Errorf("%q: %v", name, err)
}
defer file.Close()
return readerToLines(file), nil
}
func getWordLinesFromInternal(name string) ([]string, error) {
lines := linesFromInternalList(name)
if lines == nil {
return []string{}, fmt.Errorf("list %q does not exist", name)
}
return lines, nil
}