More efficient way of checking parameters

I'm very new to Pascal, been learning about it for around 4hrs now. Below is the start of a program that is in the works. It will be a command line application taking three parameters: start ip (sIp) end ip (eIp) and a DNS server address.

I would like the application to check each parameter and either assign a value from the parameter or exit if invalid data is passed to the application. As you can see the nested if statements are not the most efficient way of checking for invalid input. Could someone please demonstrate a more efficient way of performing this task?

I've tried using the case statement to assign the parameters as a variable and or to logically check data passed to the application. Of course neither of these attempts worked because of the data-types used.

Comments

: Hello,: : I'm very new to Pascal, been learning about it for around 4hrs now. : Below is the start of a program that is in the works. It will be a : command line application taking three parameters: start ip (sIp) end : ip (eIp) and a DNS server address.: : [code]: : : program dnsaudit;: : uses: sockets, inetaux;: : : var: i: integer;: sIp, eIp: AnsiString;: gtOpt: Pchar;: : begin (* Main *): : for i := 0 to ParamCount do: begin: if ParamStr(i) = '-s' then: begin: sIp := ParamStr(i +1);: writeln(sIp);: end;: if ParamStr(i) = '-e' then: begin: eIp := ParamStr(i +1);: writeln(eIp);: end: else : begin: if ParamStr(i) <> '-s' then: begin: if ParamStr(i) <> '-e' then: begin: if ParamStr(i) <> eIp then: begin : if ParamStr(i) <> sIp then: begin: writeln('Invalid parameter or input specified, exiting...');: exit();: end: : end: end: : end: end;: end;: : //writeln(ParamCount);: : : end. (* Main *)[/code]: : : I would like the application to check each parameter and either : assign a value from the parameter or exit if invalid data is passed : to the application. As you can see the nested if statements are not : the most efficient way of checking for invalid input. Could someone : please demonstrate a more efficient way of performing this task?: : I've tried using the case statement to assign the parameters as a : variable and or to logically check data passed to the application. : Of course neither of these attempts worked because of the data-types : used.: : : You could use an if-then-else-if-then-else-construction:[code] if ParamStr(i) = '-s' then begin end else if ParamStr(i) = '-e' then begin end else begin // ParamStr(i) is neither a -e nor a -s end;[/code]It is also better to use a while-loop instead of a for-loop, because you can control the counter:[code] [b]i := 1;[/b] while i <= ParamCount do begin if (ParamStr(i) = '-s') and (i <ParamCount) then begin i++; data = ParamStr(i); end else if ParamStr(i) = '-e' then begin i++; data = ParamStr(i); end else begin // Error in parameters end; i++; end;[/code]Also note how i begins at 1. ParamStr 0 = executable name.

Here the "main" part of the program merely does all the housekeeping and then turns the actual work over to the procedure [b]DoIt[/b]. The first thing it does is to check whether there are at least three parameters on the command line. If there are more then the extra ones are ignored. If there are fewer then presumably the program does hot have enough data to do its job and the program terminates with the message 'Not enough parameters!' I prefer to put the termination message in the [b]else[/b] part of the [b]if[/b] statement instead of using [b]halt[/b] or [b]exit[/b] to bug out. Whatever.

Validity of each parameter is embodied in three boolean functions [b]sIp_Valid, eIp_Valid[/b] and [b]DNS_Valid[/b]. This assumes that the tests for each parameter are unique. If they are the same then one Function could be defined and called thus:[code] if Valid(ParamStr(1)) and Valid(ParamStr(2)) and Valid(ParamStr(3)) then[/code]

I've assumed that the values of sIp, eIP and Dns are simply ParamStr(1), ParamStr(2) and ParamStr(3). If not then functions for converting the parameters can be written and called:[code] DoIt (eIpVal(ParamStr(1)), sIpVal(ParamStr(2)), DnsVal(ParamStr(3)) ; [/code]

Approaching it this way the main part of the program is only 15 lines and the complexities are shuffled off to other procedures and functions where the can be handled one at at time. The procedure [b]DoIt[/b] can repeat this strategy and ultimately the entire program can consist of simple, easily understood modules.