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 10from argparse import ArgumentParser 11import sys 12 13 14def print_and_exit(msg): 15 sys.stderr.write(msg + "\n") 16 sys.exit(1) 17 18 19def main(): 20 parser = ArgumentParser(description="Concatenate two files into a single file") 21 parser.add_argument( 22 "-o", 23 "--output", 24 dest="output", 25 required=True, 26 help="The output file. stdout is used if not given", 27 type=str, 28 action="store", 29 ) 30 parser.add_argument( 31 "files", metavar="files", nargs="+", help="The files to concatenate" 32 ) 33 34 args = parser.parse_args() 35 36 if len(args.files) < 2: 37 print_and_exit("fewer than 2 inputs provided") 38 data = "" 39 for filename in args.files: 40 with open(filename, "r") as f: 41 data += f.read() 42 if len(data) != 0 and data[-1] != "\n": 43 data += "\n" 44 assert len(data) > 0 and "cannot cat empty files" 45 with open(args.output, "w") as f: 46 f.write(data) 47 48 49if __name__ == "__main__": 50 main() 51 sys.exit(0) 52