Results 1 to 4 of 4
- 12-29-2009, 03:17 AM #1
managing arguments passed to program
Howdy everyone!
I have a functional piece of code that works, but as i keep adding to it, i am not sure that it allows for fair scalability. Basically it looks close to the one below:
My program receives Strings, String array, Boolean etc as part of the arguments. I need a way to *efficiently* go through the arguments list and assign variables that follow arguments.
Here is a sample of arguments:
-sFile <filename>, dFiles <files1>,<file2>,<file3> -outputFile <file> -db yes -sortResults yes
Program *may* receive all or some (I need to account for this somehow)
Order matters. I really would prefer it did not.
What i really would like your help with is an approach that is more efficient then this and allows for an easy way to add new arguments and remove old ones.
Thank you in advance
My code is very close to this:
Java Code:public static void main(String[] args) { for (int i = 0; i < args.length; i++) { { if (args[i].equals("-Sb")) { String a = args[i+1]; //could be a string } else if (args[i].equals("-Lk")) { String[] b = args[i+1].split(","); // or a comma delimited array } else if (args[i].equals("-Oj")) { if (args[i+1].toUpperCase() != null) { callSomeOtherMethodAndDoSomething(); //deviate from args check and do something } } } } }
- 12-29-2009, 05:16 AM #2gcampton Guest
Well, personally I haven't come up with a better method, however typically I use a separate method rather than Main and I use a switch rather than complex if/else. eg.
either way it doesn't make too much difference whether you use a separate method or not, but the switch can make it look a bit nicer. and also allows for easier editing
Java Code:public class Main { public static void main(String[] args) { int choice=0; final int QUIT=-2; processArgs(args); MyProgram ap = new MyProgram(); while(choice != QUIT) { choice = ap.runMainMenu(); } } public static void processArgs(String[] args); { switch (args) { case '-help': displayHelp(); break; ...... ...... } } }Last edited by gcampton; 12-29-2009 at 05:19 AM.
- 12-30-2009, 04:38 AM #3
But you can't switch on strings, can you? Numbers, characters and enums only I thought
Can you post a more complete example?
- 12-30-2009, 10:32 AM #4gcampton Guest
Similar Threads
-
entities are passed by value or passed by reference
By syntrax in forum New To JavaReplies: 1Last Post: 12-17-2009, 07:13 AM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
Star character in program arguments
By CodesAway in forum EclipseReplies: 2Last Post: 10-28-2009, 02:01 PM -
Managing jPanels in jSplitPane
By calexander in forum Advanced JavaReplies: 6Last Post: 11-13-2008, 07:06 PM -
Running java program with arguments in Unix"
By gvi in forum Advanced JavaReplies: 2Last Post: 11-08-2007, 07:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks