Browse Source

Add multiple input files

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
master
Rémi BERTHO 4 years ago
parent
commit
bf0b75dfc1
Signed by: dalan GPG Key ID: EE3B917931C07B64
  1. 29
      SSnR.py

29
SSnR.py

@ -24,6 +24,7 @@
import sys
import os.path
from os import walk
import argparse
import regex
@ -35,12 +36,15 @@ def main():
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, nargs='+')
parser.add_argument('-p', '--regex_input', help='Regex input file', required=False)
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',
required=False, action='store_true')
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
required=False, action='store_true')
parser.add_argument('-u', '--recursive', help='Use the regex input recrusivly in the folders',
required=False, action='store_true')
args = vars(parser.parse_args())
@ -56,14 +60,33 @@ def main():
# Get input
filenames = []
if args["input"] is not None:
if args["regex_input"] is not None:
try:
input_ex = compile_regex(args["regex_input"])
except SyntaxError as exception:
print("Error when compiling input regex: " + str(exception))
return -1
except regex.error as exception:
print("Error when compiling input regex: " + exception.msg)
return -1
for (dirpath, dirnames, dir_filenames) in walk("."):
for filename in dir_filenames:
if input_ex.fullmatch(filename):
filenames.append(os.path.join(dirpath, filename))
is_file = True
if not args["recursive"]:
break
if not filenames:
print("Error: no input file")
return -1
elif args["input"] is not None:
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: file not found: " + str(args["input"]))
if not filenames:
print("Error: no input file")
return -1
elif args["string"] is not None:

Loading…
Cancel
Save