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