Results 1 to 4 of 4
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default 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
    					}
    				}
    			}
    
    		}
    	}

  2. #2
    gcampton Guest

    Default

    Quote Originally Posted by mac View Post
    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.
    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 06:19 AM.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    But you can't switch on strings, can you? Numbers, characters and enums only I thought

    Can you post a more complete example?

  4. #4
    gcampton Guest

    Default

    woops, your right I didn't even realize switches were that restricted.

    as normally I use chars to signify a switch or flag command

    well nothing wrong with what your doing anyway. I honestly can't see a better way of doing it.

Similar Threads

  1. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 08:13 AM
  2. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  3. Star character in program arguments
    By CodesAway in forum Eclipse
    Replies: 2
    Last Post: 10-28-2009, 03:01 PM
  4. Managing jPanels in jSplitPane
    By calexander in forum Advanced Java
    Replies: 6
    Last Post: 11-13-2008, 08:06 PM
  5. Running java program with arguments in Unix"
    By gvi in forum Advanced Java
    Replies: 2
    Last Post: 11-08-2007, 08:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •