Home | History | Annotate | Line # | Download | only in utils
      1 #!/usr/bin/env python
      2 #===----------------------------------------------------------------------===##
      3 #
      4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      5 # See https://llvm.org/LICENSE.txt for license information.
      6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      7 #
      8 #===----------------------------------------------------------------------===##
      9 
     10 from argparse import ArgumentParser
     11 import sys
     12 
     13 def print_and_exit(msg):
     14     sys.stderr.write(msg + '\n')
     15     sys.exit(1)
     16 
     17 def main():
     18     parser = ArgumentParser(
     19         description="Concatenate two files into a single file")
     20     parser.add_argument(
     21         '-o', '--output', dest='output', required=True,
     22         help='The output file. stdout is used if not given',
     23         type=str, action='store')
     24     parser.add_argument(
     25         'files', metavar='files',  nargs='+',
     26         help='The files to concatenate')
     27 
     28     args = parser.parse_args()
     29 
     30     if len(args.files) < 2:
     31         print_and_exit('fewer than 2 inputs provided')
     32     data = ''
     33     for filename in args.files:
     34         with open(filename, 'r') as f:
     35             data += f.read()
     36         if len(data) != 0 and data[-1] != '\n':
     37             data += '\n'
     38     assert len(data) > 0 and "cannot cat empty files"
     39     with open(args.output, 'w') as f:
     40         f.write(data)
     41 
     42 
     43 if __name__ == '__main__':
     44     main()
     45     sys.exit(0)
     46