diff --git a/SSnR.py b/SSnR.py index 408a84e..f07cbdf 100755 --- a/SSnR.py +++ b/SSnR.py @@ -23,6 +23,7 @@ # import sys +import os.path import argparse import regex @@ -33,7 +34,7 @@ def main(): parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR') parser.add_argument('-e', '--regex', help='Regex', required=True) parser.add_argument('-s', '--string', help='String', required=False) - parser.add_argument('-i', '--input', help='Input file', required=False) + parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+') parser.add_argument('-o', '--output', help='Output file', required=False) parser.add_argument('-r', '--replace', help='Replace', required=False) parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace', @@ -43,15 +44,28 @@ def main(): args = vars(parser.parse_args()) + # Compile regex + try: + ex = compile_regex(args["regex"]) + except SyntaxError as exception: + print("Error when compiling regex: " + str(exception)) + return -1 + except regex.error as exception: + print("Error when compiling regex: " + exception.msg) + return -1 + # Get input + filenames = [] if args["input"] is not None: - try: - with open(args["input"], "r") as file: - string = file.read() - except OSError as exception: - print("Error: file not found: " + str(exception)) + for input_file in args["input"]: + if os.path.isfile(input_file): + is_file = True + filenames.append(input_file) + else: + print("Error: file not found: " + args["input"]) + if filenames.count == 0: + print("Error: no input file") return -1 - is_file = True elif args["string"] is not None: string = args["string"] is_file = False @@ -70,25 +84,34 @@ def main(): else: use_output_file = False - # Compile regex - try: - ex = compile_regex(args["regex"]) - except SyntaxError as exception: - print("Error when compiling regex: " + str(exception)) - return -1 - except regex.error as exception: - print("Error when compiling regex: " + exception.msg) - return -1 - # Search or replace - if args["replace"] is not None: - replace_string = replace(ex, string, args["replace"], args["print_nb_match"]) - if use_output_file: - output_file.write(replace_string) - else: - print(replace_string) + if is_file: + for filename in filenames: + try: + with open(filename, "r") as file: + string = file.read() + except OSError as exception: + print("Error: file not found: " + str(exception)) + continue + + if args["replace"] is not None: + replace_string = replace(ex, string, args["replace"], args["print_nb_match"]) + if use_output_file: + output_file.write(replace_string) + else: + print(replace_string) + else: + print("File: " + filename) + search(ex, string, is_file) else: - search(ex, string, is_file) + if args["replace"] is not None: + replace_string = replace(ex, string, args["replace"], args["print_nb_match"]) + if use_output_file: + output_file.write(replace_string) + else: + print(replace_string) + else: + search(ex, string, is_file) return 0 def compile_regex(ex): @@ -123,7 +146,7 @@ def search(ex, string, is_file): else: print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0) + 1) + ":" + str(match.end(0)) + "]") - print("Number of match: " + str(nb_match)) + print(" - Number of match: " + str(nb_match)) def replace(ex, string, replace_string, print_nb):