forked from itchio/wizardry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (58 loc) · 1.75 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 golibmagic
import (
"log"
"os"
kingpin "github.com/alecthomas/kingpin/v2"
)
var (
app = kingpin.New("magic", "A magic parser/interpreter/compiler")
compileCmd = app.Command("compile", "Compile a set of magic files into one .go file")
identifyCmd = app.Command("identify", "Use a magic file to identify a target file")
)
var appArgs = struct {
debugParser *bool
debugInterpreter *bool
}{
app.Flag("debug-parser", "Turn on verbose parser output").Bool(),
app.Flag("debug-interpreter", "Turn on verbose interpreter output").Bool(),
}
var identifyArgs = struct {
magdir *string
target *string
}{
identifyCmd.Arg("magdir", "the folder of magic files to compile").Required().String(),
identifyCmd.Arg("target", "path of the the file to identify").Required().String(),
}
var compileArgs = struct {
magdir *string
output *string
chatty *bool
emitComments *bool
pkg *string
}{
compileCmd.Arg("magdir", "the folder of magic files to compile").Required().String(),
compileCmd.Flag("output", "the go file to generate").Short('o').Required().String(),
compileCmd.Flag("chatty", "generate prints on every rule match").Bool(),
compileCmd.Flag("emit-comments", "generate comments in the code").Bool(),
compileCmd.Flag("package", "go package to generate").Default("main").String(),
}
func main() {
app.HelpFlag.Short('h')
app.Author("Amos Wenger <amos@itch.io>")
cmd, err := app.Parse(os.Args[1:])
if err != nil {
ctx, _ := app.ParseContext(os.Args[1:])
app.FatalUsageContext(ctx, "%s\n", err.Error())
}
switch kingpin.MustParse(cmd, err) {
case compileCmd.FullCommand():
must(doCompile())
case identifyCmd.FullCommand():
must(doIdentify())
}
}
func must(err error) {
if err != nil {
log.Fatalf("%+v", err)
}
}