Improve parsing arguments
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
cc54900498
commit
da4a6a7828
1 changed files with 38 additions and 27 deletions
55
SSnR.py
55
SSnR.py
|
@ -33,27 +33,38 @@ def main():
|
|||
"""
|
||||
Main function
|
||||
"""
|
||||
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
||||
parser.add_argument('-ex', '--regex', help='Regex', required=True)
|
||||
parser.add_argument('-str', '--string', help='Input string', required=False)
|
||||
parser.add_argument('-if', '--input', help='Input file', required=False, nargs='+')
|
||||
parser.add_argument('-iex', '--regex_input', help='Regex input file', required=False)
|
||||
parser.add_argument('-oex', '--regex_output', help='Regex output file', required=False)
|
||||
parser.add_argument('-of', '--output', help='Output file', required=False)
|
||||
parser.add_argument('-rex', '--replace', help='Replace', required=False)
|
||||
parser.add_argument('-pm', '--print_nb_match', help='Print the number of match in replace',
|
||||
# Parse arguments
|
||||
parser = argparse.ArgumentParser(description='Search and replace tool for UTF-8 files',
|
||||
prog='SSnR')
|
||||
|
||||
regex_group = parser.add_argument_group('Regular expression', "Search and replace regular expression")
|
||||
regex_group.add_argument('-ex', '--regex', help='Regex', required=True)
|
||||
regex_group.add_argument('-rex', '--replace', help='Replace', required=False)
|
||||
|
||||
input_group = parser.add_argument_group('Input', "Input arguments, if none set use stdin")
|
||||
input_group.add_argument('-if', '--input_file', help='Input file', required=False, nargs='+')
|
||||
input_group.add_argument('-iex', '--input_regex', help='Regex input file', required=False)
|
||||
input_group.add_argument('-str', '--input_string', help='Input string', required=False)
|
||||
input_group.add_argument('-ic', '--input_clipboard', help='Use the clipboard as input',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-igc', '--ignore_case', help='Ignore the case',
|
||||
|
||||
output_group = parser.add_argument_group('Output', "In replace mode, ouput arguments, if none set use stdout")
|
||||
output_group.add_argument('-oex', '--output_regex', help='Regex output file', required=False)
|
||||
output_group.add_argument('-of', '--output_file', help='Output file', required=False)
|
||||
output_group.add_argument('-oc', '--output_clipboard', help='Use the clipboard as output',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-r', '--recursive', help='Use the regex input recrusivly in the folders',
|
||||
|
||||
option_group = parser.add_argument_group('Options', "Some options")
|
||||
option_group.add_argument('-pm', '--print_nb_match', help='Print the number of match in replace',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-ic', '--input_clipboard', help='Use the clipboard as input',
|
||||
option_group.add_argument('-igc', '--ignore_case', help='Ignore the case',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-oc', '--output_clipboard', help='Use the clipboard as output',
|
||||
option_group.add_argument('-r', '--recursive', help='Use the regex input recrusivly in the folders',
|
||||
required=False, action='store_true')
|
||||
|
||||
args = vars(parser.parse_args())
|
||||
|
||||
|
||||
# Compile regex
|
||||
try:
|
||||
ex = compile_regex(args["regex"], args["ignore_case"])
|
||||
|
@ -66,9 +77,9 @@ def main():
|
|||
|
||||
# Get input
|
||||
input_filenames = []
|
||||
if args["regex_input"] is not None:
|
||||
if args["input_regex"] is not None:
|
||||
try:
|
||||
input_ex = compile_regex(args["regex_input"], False)
|
||||
input_ex = compile_regex(args["input_regex"], False)
|
||||
except SyntaxError as exception:
|
||||
print("Error when compiling input regex: " + str(exception))
|
||||
return -1
|
||||
|
@ -85,8 +96,8 @@ def main():
|
|||
if not input_filenames:
|
||||
print("Error: no input file")
|
||||
return -1
|
||||
elif args["input"] is not None:
|
||||
for input_file in args["input"]:
|
||||
elif args["input_file"] is not None:
|
||||
for input_file in args["input_file"]:
|
||||
if os.path.isfile(input_file):
|
||||
is_file = True
|
||||
input_filenames.append(input_file)
|
||||
|
@ -95,8 +106,8 @@ def main():
|
|||
if not input_filenames:
|
||||
print("Error: no input file")
|
||||
return -1
|
||||
elif args["string"] is not None:
|
||||
string = args["string"]
|
||||
elif args["input_string"] is not None:
|
||||
string = args["input_string"]
|
||||
is_file = False
|
||||
elif args["input_clipboard"]:
|
||||
string = pyperclip.paste()
|
||||
|
@ -107,14 +118,14 @@ def main():
|
|||
|
||||
# Get output
|
||||
output_filenames = []
|
||||
if args["regex_output"] is not None:
|
||||
if args["output_regex"] is not None:
|
||||
if input_ex is None:
|
||||
print("Error: You need a regex input file to use a regex output file")
|
||||
return -1
|
||||
for input_filename in input_filenames:
|
||||
output_filenames.append(input_ex.subn(args["regex_output"], input_filename)[0])
|
||||
output_filenames.append(input_ex.subn(args["output_regex"], input_filename)[0])
|
||||
use_output_file = True
|
||||
elif args["output"] is not None:
|
||||
elif args["output_file"] is not None:
|
||||
output_filenames.append(args["output"])
|
||||
use_output_file = True
|
||||
elif args["output_clipboard"]:
|
||||
|
|
Loading…
Reference in a new issue