Skip to content

Commit 43a3455

Browse files
committed
list: add -o/--output <file> option to output to file [GH-14]
1 parent b3eeeef commit 43a3455

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

compdb/cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import print_function, unicode_literals, absolute_import
22

33
import argparse
4+
import io
45
import logging
56
import os
67
import 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

Comments
 (0)