Skip to content

Commit 37edef4

Browse files
committed
Add -f flag to specify output file
1 parent f201c9f commit 37edef4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gotags is a [ctags][]-compatible tag generator for [Go][].
1717

1818
-L="": source file names are read from the specified file.
1919
-R=false: recurse into directories in the file list.
20+
-f="": write output to specified file. If file is "-", output is written to standard out.
2021
-silent=false: do not produce any output on error.
2122
-sort=true: sort tags.
2223
-v=false: print version.

main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"flag"
66
"fmt"
7+
"io"
78
"os"
89
"path/filepath"
910
"sort"
@@ -22,6 +23,7 @@ const (
2223
var (
2324
printVersion bool
2425
inputFile string
26+
outputFile string
2527
recurse bool
2628
sortOutput bool
2729
silent bool
@@ -31,6 +33,7 @@ var (
3133
func init() {
3234
flag.BoolVar(&printVersion, "v", false, "print version.")
3335
flag.StringVar(&inputFile, "L", "", "source file names are read from the specified file.")
36+
flag.StringVar(&outputFile, "f", "", `write output to specified file. If file is "-", output is written to standard out.`)
3437
flag.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
3538
flag.BoolVar(&sortOutput, "sort", true, "sort tags.")
3639
flag.BoolVar(&silent, "silent", false, "do not produce any output on error.")
@@ -161,8 +164,23 @@ func main() {
161164
sort.Sort(sort.StringSlice(output))
162165
}
163166

167+
var out io.Writer
168+
if len(outputFile) == 0 || outputFile == "-" {
169+
// For compatibility with older gotags versions, also write to stdout
170+
// when outputFile is not specified.
171+
out = os.Stdout
172+
} else {
173+
file, err := os.Create(outputFile)
174+
if err != nil {
175+
fmt.Fprintf(os.Stderr, "could not create output file: %s\n", err)
176+
os.Exit(1)
177+
}
178+
out = file
179+
defer file.Close()
180+
}
181+
164182
for _, s := range output {
165-
fmt.Println(s)
183+
fmt.Fprintln(out, s)
166184
}
167185
}
168186

0 commit comments

Comments
 (0)