1
1
from __future__ import print_function , unicode_literals , absolute_import
2
2
3
3
import argparse
4
+ import io
4
5
import logging
5
6
import os
6
7
import sys
@@ -77,18 +78,26 @@ def execute(self, config, argv):
77
78
'--unique' ,
78
79
action = 'store_true' ,
79
80
help = 'restrict results to a single entry per file' )
81
+ parser .add_argument (
82
+ '-o' ,
83
+ '--output' ,
84
+ metavar = 'file' ,
85
+ help = 'write to file instead of stdout' )
80
86
parser .add_argument (
81
87
'files' ,
82
88
metavar = 'file' ,
83
89
nargs = '*' ,
84
90
help = 'restrict results to a list of files' )
85
91
args = parser .parse_args (argv )
92
+ if args .output :
93
+ output_writer = io .open (args .output , 'w' , encoding = 'utf8' )
94
+ else :
95
+ output_writer = utils .stdout_unicode_writer ()
86
96
has_missing_files = False
87
97
database = self ._make_database (config )
88
98
builder = compdb .includedb .IncludeIndexBuilder ()
89
99
included_by_database = builder .build (database )
90
- with JSONCompileCommandSerializer (
91
- utils .stdout_unicode_writer ()) as serializer :
100
+ with JSONCompileCommandSerializer (output_writer ) as serializer :
92
101
for file , compile_commands in self ._gen_results (
93
102
database , included_by_database , args ):
94
103
has_compile_command = False
@@ -100,6 +109,8 @@ def execute(self, config, argv):
100
109
'error: {}: no such entry' .format (file ),
101
110
file = sys .stderr )
102
111
has_missing_files = True
112
+ if args .output :
113
+ output_writer .close ()
103
114
if has_missing_files :
104
115
sys .exit (1 )
105
116
0 commit comments