Board index » delphi » How To: Command line args. eg pasprg arg1 arg2..argN??

How To: Command line args. eg pasprg arg1 arg2..argN??

Hi,
I hope this will be an easy one for someone.
I am changing a pascal program and being a non-pascal person I have not
been able to work out how to pass some variables from the command line
and process them within the code. ie as in argv and argc in C.

eg if pasprg is the name of the program then from the command line
I want to type "pasprg 4 8 9" and have the 4,8 and 9 end up in variables
so I can process them at runtime.
I do not want to prompt for them with write/readln

Any help and esp a small example would be greatly appreciated.

TIA
--
Brendan Charles Programmer/Analyst/All round good guy  |Things are always
Lactos Pty Ltd - Master Cheesemakers - Australia       |found in the last place
Ph  +61 04 312566 Fax +61 04 312647 TZ=GMT+10hrs       |we look - for who
bchar...@lawson.its.utas.edu.au                        |searches after finding?

 

Re:How To: Command line args. eg pasprg arg1 arg2..argN??


Brendan Patrick Charles <bchar...@lawson.its.utas.edu.au> wrote:

Quote
> I am changing a pascal program and being a non-pascal person I have not
> been able to work out how to pass some variables from the command line
> and process them within the code. ie as in argv and argc in C.
> eg if pasprg is the name of the program then from the command line
> I want to type "pasprg 4 8 9" and have the 4,8 and 9 end up in variables
> so I can process them at runtime.
> I do not want to prompt for them with write/readln
> Any help and esp a small example would be greatly appreciated.

You can use ParamStr to pass the command line parameters to the program,
and use ParamCount for the number of parameters passed.

here's a short example:

Program Param_Example;
Var
  ParamNum : Integer;

Begin
  For ParamNum := 1 To ParamCount Do
    WriteLn('Parameter ',ParamNum,': ',ParamStr(ParamNum));
End.

 +---------------------+-------------------------+-----------------------------+
 | Zulkarnain Shariff  | z...@kmpssg.po.my                                      |
 +---------------------+-------------------------+-----------------------------+
 | Computer Programmer | KOMPASS South-East Asia | Singapore                   |
 +---------------------+-------------------------+-----------------------------+

 +---------------------+-------------------------------------------------------+
 | Zulkarnain Shariff  | zu...@singnet.com.sg                                  |
 +---------------------+------------------------+------------------------------+
 | Operations Manager  | NetBase Computers (S)  | Singapore                    |
 |                     | SoftBase Computing (M) | Kuala Lumpur, Malaysia       |
 +---------------------+------------------------+------------------------------+
 Standard_Disclamer:
 The opinions expressed are strictly on my own, none of which reflects the
 interest of the companies stated above, unless otherwise stated.

Re:How To: Command line args. eg pasprg arg1 arg2..argN??


bchar...@lawson.its.utas.edu.au (Brendan Patrick Charles) wrote:

Quote
>Hi,
>I hope this will be an easy one for someone.
>I am changing a pascal program and being a non-pascal person I have not
>been able to work out how to pass some variables from the command line
>and process them within the code. ie as in argv and argc in C.

>eg if pasprg is the name of the program then from the command line
>I want to type "pasprg 4 8 9" and have the 4,8 and 9 end up in variables
>so I can process them at runtime.
>I do not want to prompt for them with write/readln

Piece of cake, mate:

Number of parameters is to be found in ParamCount and the parameters
them selves in ParamStr so, in your example above, ParamCount=3 and
ParamStr(1)=4, ParamStr(2)=8 and ParamStr(3)=9.

ParamStr(0) always exists and is the name, with full path, of the
executing program. Note that this is only true when you run the
program from command line. What you get inside the IDE varies with the
version.

... DH

Other Threads