Well, all the args come as String first and they will (or should) all start with "-". So, chop the first character of the String (if it is not "-" then return an error and exit). Then chop the next character (charAt()) and run that through a switch statement.
i.e.
int n;
File i;
for (String arg : args) {
if (arg.charAt(0) != '-') {
System.err.println("whatever");
System.exit(1);
}
switch (arg.charAt(1)) {
case 'n':
n = Integer.parseInt(arg.substring(2));
break;
case 'i':
i = new File(arg.substring(2));
break;
default:
System.err.println("Invalid .....");
System.exit(1);
break;
}
}
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
I think there's a library in the Jakarta Commons project that's supposed to pretty much automate commandline parameter parsing.
Whether it's easier to use that than parse them yourself I can't tell as I've not used it ;)
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
I think there's a library in the Jakarta Commons project that's supposed to pretty much automate commandline parameter parsing.
Whether it's easier to use that than parse them yourself I can't tell as I've not used it ;)
I should have known, but then again I don't deal with this too often either, so I haven't bothered looking for an API. But, if its a "common" task, jakarta commons usually has something for it. ;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494