I am working on a project in which I need to pass multiple arguments from the command line-
Below is the use case I have-
From the command line, I will be passing atleast four paramaters-
noOfThreads
,noOfTasks
,startRange
andtableName1
, so if I am passing these four thing, then I need to store them in a variable and for any table names- I need to add it into the string list so that I can use them in my other code.Secondly, I can pass five parameters instead of four just like above- So five parameters can be-
noOfThreads
,noOfTasks
,startRange
,tableName1
andtableName2
. So heretableName2
is extra. so if I am passing these five thing, then I need to store them in a variable and here I am passingtableName1
andtableName2
as two tables so I will be storing these two tables in a string list.Thirdly, I can pass six parameters instead of five just like above- So sixparameters can be-
noOfThreads
,noOfTasks
,startRange
,tableName1
,tableName2
andtableName3
. So heretableName3
is extra. so if I am passing these six thing, then I need to store them in a variable and here I am passingtableName1
,tableName2
andtableName3
as three tables so I will be storing these three tables in a string list again.
So for above scenario, I have the below code with me. It is looking very ugly currently as I have lot of repetition in my code as mentioned below. Is there any way I can make it more cleaner?
Below is my code-
private static List<String> databaseNames = new ArrayList<String>();
private static int noOfThreads;
private static int noOfTasks;
private static int startRange;
private static String tableName1;
private static String tableName2;
private static String tableName3;
public static void main(String[] args) {
if (args.length > 0 && args.length < 5) {
noOfThreads = Integer.parseInt(args[0]);
noOfTasks = Integer.parseInt(args[1]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
databaseNames.add(tableName1);
} else if (args.length > 0 && args.length < 6) {
noOfThreads = Integer.parseInt(args[0]);
noOfTasks = Integer.parseInt(args[1]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
tableName2 = args[4];
databaseNames.add(tableName1);
databaseNames.add(tableName2);
} else {
noOfThreads = Integer.parseInt(args[0]);
noOfTasks = Integer.parseInt(args[1]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
tableName2 = args[4];
tableName3 = args[5];
databaseNames.add(tableName1);
databaseNames.add(tableName2);
databaseNames.add(tableName3);
}
}