Add multiple input files
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
9e48368148
commit
bf0b75dfc1
1 changed files with 26 additions and 3 deletions
29
SSnR.py
29
SSnR.py
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os.path
|
import os.path
|
||||||
|
from os import walk
|
||||||
import argparse
|
import argparse
|
||||||
import regex
|
import regex
|
||||||
|
|
||||||
|
@ -35,12 +36,15 @@ def main():
|
||||||
parser.add_argument('-e', '--regex', help='Regex', required=True)
|
parser.add_argument('-e', '--regex', help='Regex', required=True)
|
||||||
parser.add_argument('-s', '--string', help='String', required=False)
|
parser.add_argument('-s', '--string', help='String', required=False)
|
||||||
parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+')
|
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('-o', '--output', help='Output file', required=False)
|
||||||
parser.add_argument('-r', '--replace', help='Replace', 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',
|
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
|
||||||
required=False, action='store_true')
|
required=False, action='store_true')
|
||||||
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
|
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
|
||||||
required=False, action='store_true')
|
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())
|
args = vars(parser.parse_args())
|
||||||
|
|
||||||
|
@ -56,14 +60,33 @@ def main():
|
||||||
|
|
||||||
# Get input
|
# Get input
|
||||||
filenames = []
|
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"]:
|
for input_file in args["input"]:
|
||||||
if os.path.isfile(input_file):
|
if os.path.isfile(input_file):
|
||||||
is_file = True
|
is_file = True
|
||||||
filenames.append(input_file)
|
filenames.append(input_file)
|
||||||
else:
|
else:
|
||||||
print("Error: file not found: " + args["input"])
|
print("Error: file not found: " + str(args["input"]))
|
||||||
if filenames.count == 0:
|
if not filenames:
|
||||||
print("Error: no input file")
|
print("Error: no input file")
|
||||||
return -1
|
return -1
|
||||||
elif args["string"] is not None:
|
elif args["string"] is not None:
|
||||||
|
|
Loading…
Reference in a new issue