# Passing multiple parameters from the command line in Java

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-

1. From the command line, I will be passing atleast four paramaters- noOfThreads, noOfTasks, startRange and tableName1, 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.

2. Secondly, I can pass five parameters instead of four just like above- So five parameters can be- noOfThreads, noOfTasks, startRange , tableName1 and tableName2. So here tableName2 is extra. so if I am passing these five thing, then I need to store them in a variable and here I am passing tableName1 and tableName2 as two tables so I will be storing these two tables in a string list.

3. Thirdly, I can pass six parameters instead of five just like above- So sixparameters can be- noOfThreads, noOfTasks, startRange , tableName1, tableName2 and tableName3. So here tableName3 is extra. so if I am passing these six thing, then I need to store them in a variable and here I am passing tableName1 , tableName2 and tableName3 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 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) {

startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
} else if (args.length > 0 && args.length < 6) {
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
tableName2 = args[4];
} else {
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
tableName2 = args[4];
tableName3 = args[5];
}
}

-

1. Extract out the common logic before the if. The following is more or less the same:

noOfThreads = Integer.parseInt(args[0]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
if (args.length >= 5) {
tableName2 = args[4];
}
if (args.length >= 6) {
tableName3 = args[5];
}

2. Note that running the program with less than five parameters only prints an exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ...Main.main(Main.java:16)


You should handle this and print a user friendly error message or usage help.

-
@Glenn Rogers: Thanks for the edit! – palacsint Feb 10 '13 at 7:41

It might be worth checking out Commons CLI. It's a library that handles complicated command-line arguments and provides a lot of nice stuff like pretty error messages and succinct ways of specifying optional vs required arguments. Right now, your code is probably borderline on whether it will actually be simplified by this addition, but it will definitely help those who are using your code, and it will be far easier to add or modify the arguments in the future.

-
+1 for the link – cl-r Feb 12 '13 at 7:39