4
4
"bufio"
5
5
"flag"
6
6
"fmt"
7
+ "io"
7
8
"os"
8
9
"path/filepath"
9
10
"sort"
@@ -22,6 +23,7 @@ const (
22
23
var (
23
24
printVersion bool
24
25
inputFile string
26
+ outputFile string
25
27
recurse bool
26
28
sortOutput bool
27
29
silent bool
31
33
func init () {
32
34
flag .BoolVar (& printVersion , "v" , false , "print version." )
33
35
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.` )
34
37
flag .BoolVar (& recurse , "R" , false , "recurse into directories in the file list." )
35
38
flag .BoolVar (& sortOutput , "sort" , true , "sort tags." )
36
39
flag .BoolVar (& silent , "silent" , false , "do not produce any output on error." )
@@ -161,8 +164,23 @@ func main() {
161
164
sort .Sort (sort .StringSlice (output ))
162
165
}
163
166
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
+
164
182
for _ , s := range output {
165
- fmt .Println ( s )
183
+ fmt .Fprintln ( out , s )
166
184
}
167
185
}
168
186
0 commit comments