2

I have a program that calculates the factorial of multiple numbers. These nubers are passed as parameters in cmd as such :

factorial.exe 3 4 5

This will calculate the factorials of 3, 4 and 5 respectively. An earlier version of the program had a percentage that showed the fullness of the stack. I want to bring that back now, but I to also pass the wait time as a parameter in cmd as such:

factorial.exe 3 4 5 wait_time1

or

factorial.exe 3 4 5 1000

To read the numbers I use this args parser :

static string the_time;
    public static void Main(string[] args)
    {

        foreach (string s in args)
        {

            extra = int.Parse(s);

            Calculate(extra);
        }
     }

How can I separate the arguments? Thanks in advance.

robertpas
  • 643
  • 5
  • 12
  • 25

6 Answers6

4

You could add the waittime arg like a switch /t:100 so only when you see /t you know it is a waittime.

If you know your args will always have the waittime, then waittime is

waittime = arg[args.Length-1]

Probably worth a look as well

In case you don't want to reinvent the wheel

Community
  • 1
  • 1
Ian G
  • 29,468
  • 21
  • 78
  • 92
1

I think, what you want is something like getopt on UNIX-like machines to parse arguments in a sensible way. Have a look here.

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
0

Each element at the array will be a string from the command line, space-divided. This way, for

factorial.exe 3 4 5

will you have

args[0] //3
args[1] //4
args[2] //5

EDIT

Thanks to DD59, now I understand your question. You could have a convention that the last parameter will always be the wait time, or you could use a syntax, such as -time (or /t:, as he said).

factorial.exe 3 4 5 -1000

Regards

Andre Calil
  • 7,652
  • 34
  • 41
0

You are already separating the arguments in foreach loop. The string s is what you get as argument. I suppose you want to pinpoint the wait_time value and don't want to calculate it of course. You have to fix the position like - last argument is wait_time or first arugment is. Without fixing the location it will be hard to determine.

Umesh
  • 2,704
  • 19
  • 21
0

Is wait_time a mandatory argument? Does it always stand at the last place? Then you could do it like this:

the_time = args[args.Length - 1];
for (int i=0; i<args.length - 1; i++){ 
    extra = int.Parse(args[i]); 
    Calculate(extra); 
} 

If wait_time is optional, you should go with DD59's answer.

Hinek
  • 9,519
  • 12
  • 52
  • 74
  • it is mandatory and for some reason this doesn't work... If I place the time as the first parameter it works faster than it should if it's last it works slower... need to take a look at this – robertpas Aug 02 '12 at 07:32
  • The code above checks for you wait_time in the last argument. So putting it first will result in the last of your numbers being interpreted as the wait_time ... – Hinek Aug 02 '12 at 09:30
0

Parsing the command line is a quite complex part. Maybe you should take a look at a available library (some examples):

Oliver
  • 43,366
  • 8
  • 94
  • 151