group18-HW-2-3-4-5.src.cli
" Helper class for parsing arguments
1"""" 2 Helper class for parsing arguments 3""" 4import sys 5 6 7class CLI: 8 9 def __init__(self): 10 self.allowed_args = {'-d', '-e', '-f', '-h', '-n', '-s', '--eg', '--dump', '--file', '--help', '--nums', 11 '--seed'} 12 13 def parse_user_arguments(self, args): 14 15 arguments = [] 16 arguments_values = [] 17 18 # --help is an exception as we only show help and don't take input, so we process it first 19 if '-h' in args or '--help' in args: 20 showHelp() 21 args.remove("-h" if '-h' in args else "--help") 22 23 # odd indexes are cli options and even indexes are there corresponding values. 24 # we ignore the first index as it will be the filename. 25 for i in range(1, len(args)): 26 if i % 2 == 0: 27 arguments_values.append(args[i]) 28 else: 29 arguments.append(args[i]) 30 31 self.validate_user_input(arguments, arguments_values) 32 return arguments, arguments_values 33 34 def validate_user_input(self, input_arg_list, input_val_list): 35 36 if len(input_arg_list) != len(input_val_list): 37 sys.exit("The input arguments are not correct, please check!") 38 39 for argument in input_arg_list: 40 if argument not in self.allowed_args: 41 sys.exit(f"{argument} is not valid, please check!") 42 43 44def showHelp(): 45 helper_string = """ 46 CSV : summarized csv file 47 48 USAGE: python3 csv.py [OPTIONS] 49 50 OPTIONS: 51 −e −−eg start−up example = nothing 52 −d −−dump on test failure, exit with stack dump = false 53 −f −−file file with csv data = ../data/auto93.csv 54 −h −−help show help = false 55 −n −−nums number of nums to keep = 512 56 −s −−seed random number seed = 10019 57 """ 58 print(helper_string)
class
CLI:
8class CLI: 9 10 def __init__(self): 11 self.allowed_args = {'-d', '-e', '-f', '-h', '-n', '-s', '--eg', '--dump', '--file', '--help', '--nums', 12 '--seed'} 13 14 def parse_user_arguments(self, args): 15 16 arguments = [] 17 arguments_values = [] 18 19 # --help is an exception as we only show help and don't take input, so we process it first 20 if '-h' in args or '--help' in args: 21 showHelp() 22 args.remove("-h" if '-h' in args else "--help") 23 24 # odd indexes are cli options and even indexes are there corresponding values. 25 # we ignore the first index as it will be the filename. 26 for i in range(1, len(args)): 27 if i % 2 == 0: 28 arguments_values.append(args[i]) 29 else: 30 arguments.append(args[i]) 31 32 self.validate_user_input(arguments, arguments_values) 33 return arguments, arguments_values 34 35 def validate_user_input(self, input_arg_list, input_val_list): 36 37 if len(input_arg_list) != len(input_val_list): 38 sys.exit("The input arguments are not correct, please check!") 39 40 for argument in input_arg_list: 41 if argument not in self.allowed_args: 42 sys.exit(f"{argument} is not valid, please check!")
def
parse_user_arguments(self, args):
14 def parse_user_arguments(self, args): 15 16 arguments = [] 17 arguments_values = [] 18 19 # --help is an exception as we only show help and don't take input, so we process it first 20 if '-h' in args or '--help' in args: 21 showHelp() 22 args.remove("-h" if '-h' in args else "--help") 23 24 # odd indexes are cli options and even indexes are there corresponding values. 25 # we ignore the first index as it will be the filename. 26 for i in range(1, len(args)): 27 if i % 2 == 0: 28 arguments_values.append(args[i]) 29 else: 30 arguments.append(args[i]) 31 32 self.validate_user_input(arguments, arguments_values) 33 return arguments, arguments_values
def
validate_user_input(self, input_arg_list, input_val_list):
35 def validate_user_input(self, input_arg_list, input_val_list): 36 37 if len(input_arg_list) != len(input_val_list): 38 sys.exit("The input arguments are not correct, please check!") 39 40 for argument in input_arg_list: 41 if argument not in self.allowed_args: 42 sys.exit(f"{argument} is not valid, please check!")
def
showHelp():
45def showHelp(): 46 helper_string = """ 47 CSV : summarized csv file 48 49 USAGE: python3 csv.py [OPTIONS] 50 51 OPTIONS: 52 −e −−eg start−up example = nothing 53 −d −−dump on test failure, exit with stack dump = false 54 −f −−file file with csv data = ../data/auto93.csv 55 −h −−help show help = false 56 −n −−nums number of nums to keep = 512 57 −s −−seed random number seed = 10019 58 """ 59 print(helper_string)