forked from jstemmer/gotags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (84 loc) · 1.95 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
96
97
98
package main
import (
"flag"
"fmt"
"os"
"sort"
)
const (
VERSION = "1.2.0"
NAME = "gotags"
URL = "https://github.com/jstemmer/gotags"
AUTHOR_NAME = "Joel Stemmer"
AUTHOR_EMAIL = "[email protected]"
)
var (
printVersion bool
sortOutput bool
silent bool
printTree bool // for debugging
)
// Initialize flags.
func init() {
flag.BoolVar(&printVersion, "v", false, "print version")
flag.BoolVar(&sortOutput, "sort", true, "sort tags")
flag.BoolVar(&silent, "silent", false, "do not produce any output on error")
flag.BoolVar(&printTree, "tree", false, "print syntax tree (debugging)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "gotags version %s\n\n", VERSION)
fmt.Fprintf(os.Stderr, "Usage: %s [options] file(s)\n\n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if printVersion {
fmt.Printf("gotags version %s\n", VERSION)
return
}
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no file specified\n\n")
flag.Usage()
os.Exit(1)
}
if printTree {
PrintTree(flag.Arg(0))
return
}
tags := []Tag{}
for _, file := range flag.Args() {
ts, err := Parse(file)
if err != nil {
if !silent {
fmt.Fprintf(os.Stderr, "parse error: %s\n\n", err)
}
continue
}
tags = append(tags, ts...)
}
output := createMetaTags()
for _, tag := range tags {
output = append(output, tag.String())
}
if sortOutput {
sort.Sort(sort.StringSlice(output))
}
for _, s := range output {
fmt.Println(s)
}
}
// createMetaTags returns a list of meta tags.
func createMetaTags() []string {
var sorted int
if sortOutput {
sorted = 1
}
return []string{
"!_TAG_FILE_FORMAT\t2\t",
fmt.Sprintf("!_TAG_FILE_SORTED\t%d\t/0=unsorted, 1=sorted/", sorted),
fmt.Sprintf("!_TAG_PROGRAM_AUTHOR\t%s\t/%s/", AUTHOR_NAME, AUTHOR_EMAIL),
fmt.Sprintf("!_TAG_PROGRAM_NAME\t%s\t", NAME),
fmt.Sprintf("!_TAG_PROGRAM_URL\t%s\t", URL),
fmt.Sprintf("!_TAG_PROGRAM_VERSION\t%s\t", VERSION),
}
}