1 #!/usr/bin/env python 2 #===----------------------------------------------------------------------===## 3 # 4 # The LLVM Compiler Infrastructure 5 # 6 # This file is dual licensed under the MIT and the University of Illinois Open 7 # Source Licenses. See LICENSE.TXT for details. 8 # 9 #===----------------------------------------------------------------------===## 10 11 from argparse import ArgumentParser 12 import sys 13 14 def print_and_exit(msg): 15 sys.stderr.write(msg + '\n') 16 sys.exit(1) 17 18 def main(): 19 parser = ArgumentParser( 20 description="Concatenate two files into a single file") 21 parser.add_argument( 22 '-o', '--output', dest='output', required=True, 23 help='The output file. stdout is used if not given', 24 type=str, action='store') 25 parser.add_argument( 26 'files', metavar='files', nargs='+', 27 help='The files to concatenate') 28 29 args = parser.parse_args() 30 31 if len(args.files) < 2: 32 print_and_exit('fewer than 2 inputs provided') 33 data = '' 34 for filename in args.files: 35 with open(filename, 'r') as f: 36 data += f.read() 37 if len(data) != 0 and data[-1] != '\n': 38 data += '\n' 39 assert len(data) > 0 and "cannot cat empty files" 40 with open(args.output, 'w') as f: 41 f.write(data) 42 43 44 if __name__ == '__main__': 45 main() 46 sys.exit(0) 47