-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (74 loc) · 1.99 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
package main
import (
"flag"
"fmt"
"os"
"github.com/mitchellh/colorstring"
)
var (
version bool
id int
random bool
answer bool
)
func init() {
flag.BoolVar(&version, "version", false, "print version and exit")
flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
flag.IntVar(&id, "id", -1, "print riddle with id")
flag.IntVar(&id, "i", -1, "print riddle with id (shorthand)")
flag.BoolVar(&random, "random", true, "print random riddle")
flag.BoolVar(&random, "r", true, "print random riddle (shorthand)")
flag.BoolVar(&answer, "answer", false, "show answer")
flag.BoolVar(&answer, "a", false, "show answer (shorthand)")
flag.Usage = func() {
flag.PrintDefaults()
}
flag.Parse()
}
func main() {
if version {
fmt.Println("riddle version 0.1")
return
}
if !random && id == -1 {
usageAndExit("if -random=false then -id must be set", 0)
}
var riddle Riddle
if id == -1 {
riddle = GetRandomRiddle()
} else {
var foundRiddle bool
riddle, foundRiddle = GetRiddleByID(id)
if !foundRiddle {
usageAndExit(fmt.Sprintf("id of %d does not exist", id), 0)
}
}
printRiddle(riddle, answer)
}
func printRiddle(riddle Riddle, answer bool) {
fmt.Println(colorstring.Color("\n[cyan]" + "Riddle"))
fmt.Println(colorstring.Color("[cyan]" + "======"))
fmt.Println(riddle.Question + "\n\n")
if answer {
fmt.Println(colorstring.Color("[green]" + "Answer"))
fmt.Println(colorstring.Color("[green]" + "======"))
fmt.Println(riddle.Answer + "\n")
} else {
fmt.Println(colorstring.Color("[yellow]" + "Get answer"))
fmt.Println(colorstring.Color("[yellow]" + "=========="))
fmt.Printf("riddle -id %d -answer\n\n", riddle.ID)
}
}
func printError(err error) {
fmt.Println(colorstring.Color("[red]" + err.Error()))
os.Exit(1)
}
func usageAndExit(message string, exitCode int) {
if message != "" {
fmt.Fprintf(os.Stderr, colorstring.Color("[yellow]"+message))
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(exitCode)
}