-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
80 lines (70 loc) · 1.62 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
package main
import (
"asty/asty"
"flag"
"fmt"
"os"
"strings"
)
const UsageString = `Usage: asty <command> [flags]
commands:
go2json - convert go source to json
json2go - convert json to go source
help - print this message
flags:
`
func printError(err error) {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func main() {
args := os.Args
var input, output string
var indent int
var comments, positions, references bool
fs := flag.NewFlagSet("asty", flag.ExitOnError)
fs.StringVar(&input, "input", "", "input file name")
fs.StringVar(&output, "output", "", "output file name")
fs.IntVar(&indent, "indent", 0, "indentation level")
fs.BoolVar(&comments, "comments", false, "include comments")
fs.BoolVar(&positions, "positions", false, "include positions")
fs.BoolVar(&references, "references", false, "include references to reuse nodes from multiple places")
fs.Usage = func() {
fmt.Fprint(fs.Output(), UsageString)
fs.PrintDefaults()
}
if len(args) < 2 {
fs.Usage()
return
}
err := fs.Parse(args[2:])
if err != nil {
printError(err)
}
if input == "" {
input = os.Stdin.Name()
}
if output == "" {
output = os.Stdout.Name()
}
switch args[1] {
case "go2json":
indentStr := strings.Repeat(" ", indent)
err := asty.SourceToJSON(input, output, indentStr, comments, positions, references)
if err != nil {
printError(err)
}
case "json2go":
err := asty.JSONToSource(input, output, comments, positions, references)
if err != nil {
printError(err)
}
case "help":
fs.Usage()
return
default:
fmt.Printf("unknown command: %s\n", args[1])
fs.Usage()
os.Exit(1)
}
}