11from __future__ import print_function , unicode_literals , absolute_import
22
33import argparse
4+ import io
45import logging
56import os
67import sys
@@ -77,18 +78,26 @@ def execute(self, config, argv):
7778 '--unique' ,
7879 action = 'store_true' ,
7980 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' )
8086 parser .add_argument (
8187 'files' ,
8288 metavar = 'file' ,
8389 nargs = '*' ,
8490 help = 'restrict results to a list of files' )
8591 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 ()
8696 has_missing_files = False
8797 database = self ._make_database (config )
8898 builder = compdb .includedb .IncludeIndexBuilder ()
8999 included_by_database = builder .build (database )
90- with JSONCompileCommandSerializer (
91- utils .stdout_unicode_writer ()) as serializer :
100+ with JSONCompileCommandSerializer (output_writer ) as serializer :
92101 for file , compile_commands in self ._gen_results (
93102 database , included_by_database , args ):
94103 has_compile_command = False
@@ -100,6 +109,8 @@ def execute(self, config, argv):
100109 'error: {}: no such entry' .format (file ),
101110 file = sys .stderr )
102111 has_missing_files = True
112+ if args .output :
113+ output_writer .close ()
103114 if has_missing_files :
104115 sys .exit (1 )
105116
0 commit comments