Results 1 to 3 of 3

Thread: [2.0] Best Way to Handle Command-Line Arguments

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow [2.0] Best Way to Handle Command-Line Arguments

    Hi all,

    Suppose I want my executable to accept the following argument format:
    Code:
    exename -arg1 val1 -arg2 val2
    where the arguments may be in any order; some required, some optional.

    Currently I am using the args parameter of the Main method, joining it into a string, and using regular expressions to get the -k v pairs. This is rather ugly.
    I'd like to know if there is a neater way of handling this.

    Cheers

    - P

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Best Way to Handle Command-Line Arguments

    Let's assume that your app has three valid command line arguments: -a and -b that also require a value and -c that doesn't. I'd be inclined to do something like this:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
    	string[] args = Environment.GetCommandLineArgs();
    	int index = 0;
    
    	do
    	{
    		switch (args[index])
    		{
    			case "-a":
    				index++;
    				// Call a method to process the arg at index.
    				break;
    			case "-b":
    				index++;
    				// Call a method to process the arg at index.
    				break;
    			case "-c":
    				// Do whatever is appropriate.
    				break;
    			default:
    				break;
    		}
    
    		index++;
    	} while (index < args.Length);
    }
    If you wanted to ensure that each argument was not processed more than once then you could add a bit of validation:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
    	string[] args = Environment.GetCommandLineArgs();
    	List<string> processed = new List<string>();
    	int index = 0;
    
    	do
    	{
    		switch (args[index])
    		{
    			case "-a":
    				index++;
    
    				if (index < args.Length && !processed.Contains("-a"))
    				{
    					// Call a method to process the arg at index.
    					processed.Add("-a");
    				}
    
    				break;
    			case "-b":
    				index++;
    
    				if (index < args.Length && !processed.Contains("-b"))
    				{
    					// Call a method to process the arg at index.
    					processed.Add("-b");
    				}
    
    				break;
    			case "-c":
    				if (!processed.Contains("-c"))
    				{
    					// Do whatever is appropriate.
    					processed.Add("-c");
    				}
    
    				break;
    			default:
    				break;
    		}
    
    		index++;
    	} while (index < args.Length);
    }
    The code may be a bit lengthy but it is clear. Note that that will also just ignore invalid arguments. You might choose to notify the user of the error instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Best Way to Handle Command-Line Arguments

    Cheers, that looks a lot neater.

    Here's (roughly) what I used:
    Code:
    while (index < args.Length - 1)
    {
      switch (args[index++])
      {
        case "-a":
          if (a == null)
            a = args[index++];
    
          break;
    
        // etc.
    
        default:
          break;
      }
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width