I made a command line application which uses parameters. So I try to make a system like the linux command.
So I have 4 parameters, initialized to a default value, used if wrong or none parameter are given. The user can modify those parameter by calling associate options "-options".
public static void main(String [] args) {
String locParameter = "centralOffice";
String dirResult = "//home//"+System.getProperty("user.name")+"//Documents//ResultMyApp";
int periodParameter = 24;
String counterParameter = "ALL";
int i = 0;
for (String s: args) {
switch (s) {
case "-o": if (args[i+1].equalsIgnoreCase("remote")){
locParameter = "remoteOffice";
break;
} else if (args[i+1].equalsIgnoreCase("central")){
locParameter = "centralOffice";
break;
} else {
System.err.println("Unknow parameter for location, default value: centralOffice");
break;
}
case "-p": dirResult = args[i+1];
break;
case "-kpi": counterParameter = args[i+1].toUpperCase();
break;
case "-t": try {
periodParameter = Integer.parseInt(args[i+1]) ;
} catch(NumberFormatException e){
System.err.println("Error parsing parameter, default value 24 hours");
} finally {
break;
}
}
i++;
}
System.out.println("parameter choosed: " + locParameter + ", " + periodParameter + ", " + counterParameter );}
So I am wondering, is this ok? Of course it works but maybe it is not the easiest way to do it or the most optimized code.
program --
or something like that - the last time I used it, running it with--
as arguments crashed the library \$\endgroup\$dirResult
have double-slashes? \$\endgroup\$