Was wondering if someone could help quickly, will only take you a second as i'm sure it's easy!
Firstly I just want to confirm that my method of taking a commnand line argument and passing it is correct.
Secondly I want to know how to deal with argument that have no or null value. I have tried but I kept getting the error below:

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."

Here's a good snippet of the problem below and what i'm trying to do. I'm trying to specify 'age' in Main via args and pass it to 'CopyLogFIles' but allow for a blank value and if the value is blank set age to 3.

namespace Gathering_Tool
{
    public class Program
    {
        public static string age;


public void CopyLogFiles()
        {

 		Int32 days;
                days = Convert.ToInt32(age);
                Console.WriteLine("DAYS :" + days);
	//Here is want to do something with 'days', whatever that may be. The reason i'm converting a string to int is because i'm doing a comparison with dates.

	}


public static void Main(string[] args)
        {
            Program startup = new Program();
            startup.StartupChecks();

            for (int i = 0; i < args.Length; i++)
            {
                string s = args[i];
                switch (s.ToLower())
                {
                   
                    case "-l":
                        Program getlogs = new Program();
                        Program.age = args[++i];
                        getlogs.CopyLogFiles();
		//Here I want to call and pass retrieve the value for of age from command line args. I also was to set it so that if there is no value input, it set 'days' or age to a value of 3.
                        break;
                   
                    default:
                        Console.WriteLine("The parameter is unknown, please use -help or /?: " + s);
                        Environment.Exit(1);
                        break;
                }
            }

           

        }


	}
}

Thank you!
Steve

Recommended Answers

All 3 Replies

Try,

stati void Main(string []args) {
  if(args.length!=4) {
       Console.WriteLine("Insufficient parameters"); 
       return;
  }
  .....
}

I checked and aparently it's not possible to identify a null value in an argument. For exmaple if a user does no specify a value you cannot explicitly identify this? Would it be correct then to only option is to use try/catch and then force the varibalke to be a value of 3 is no value exists.

Thanks

Someone ported GetOpts over to .NET. Google around for it. It has some extensive parameter support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.