-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (55 loc) · 1.05 KB
/
main.go
File metadata and controls
66 lines (55 loc) · 1.05 KB
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
package main
import (
"fmt"
"os"
)
const USAGE = `Usage:
cheat <cheatsheet>
cheat -d <cheatsheet>
cheat -e <cheatsheet>
cheat -h
cheat -l
Cheatsheet manager. Displays, edits, and deletes cheatsheets.
Options:
-d Deletes a cheatsheet.
-e Edits a cheatsheet. If the cheatsheet does not exist, it is created.
-h Displays this help message.
-l Lists cheatsheets.
Examples:
% cheat -e grep
% cheat grep`
var cmdsMap = map[string]func(args []string){
"-d": DeleteCmd,
"-e": EditCmd,
"-h": HelpCmd,
"-l": ListCmd,
"show": ShowCmd,
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
badUsageExit("Too few arguments.")
}
if args[0][0] == '-' {
cmd, ok := cmdsMap[args[0]]
if !ok {
badUsageExit(fmt.Sprintf("Unknown flag: %s\n", args[0]))
}
cmd(args)
} else if len(args) != 1 {
badUsageExit("Too many arguments.")
} else {
cmdsMap["show"](args)
}
}
func badUsageExit(msg string) {
if msg != "" {
fmt.Println(msg)
}
fmt.Println(USAGE)
os.Exit(1)
}
func errorExit(err error) {
fmt.Println(err)
os.Exit(1)
}