59 Votes

Delphi/Lazarus: Command Line Parameter Tutorial Part 2: Receiving

Tutorial by Stefan Trost | Last update on 2023-01-27 | Created on 2012-11-26

In our first part of the command line tutorial for Delphi and Lazarus, we looked at how to pass parameters to a program. In this second part, now we want to look at how we can receive these parameters in the program to react to them and to work with them.

ParamStr and ParamCount

Two of the most important things, we need in order to access the parameters of the command line in the program, are ParamStr and ParamCount. ParamStr is a function that - similiar to an array - returns the paramater of a specified position. ParamCount is the number of passed parameters that are available. Let us look at an example:

// execute with "MyProgram.exe param1 param2"

procedure ShowParameter;
var
   i: integer;
begin
   for i := 0 to ParamCount do
     ShowMessage(ParamStr(i));
end;

// output:
// C:\Program Files\MyProgram.exe
// param1
// param2

We execute our program together with the parameters "param1" and "param2". ParamCount helps us to know how many parameters are available, the parameters itself are returned by the function ParamStr(), so that we can retrieve the parameters with the help of a loop.

Important to know: "ParamStr(0)" always contains the full path of the application, as it can theoretically  also be seen in the command line. After that, our two parameters come in "ParamStr(1)" and "ParamStr(2)".

If we call ParamStr() with a value greater than ParamCount, an empty string is returned.

CmdLine

Via the string variable CmdLine, which is also available throughout the whole program like ParamStr and ParamCount, we can access the complete text of the command line. In the example above, CmdLine would hold the string "C:\Program Files\MyProgram.exe param1 param2", for example.

FindCmdLineSwitch

Another way to look comfortable, whether a specific command line switch is in the list of parameters, is FindCmdLineSwitch. Important to know is, that FindCmdLineSwitch only finds parameters beginning with  certain (but defineable) characters. By default, these are the two characters "/" and "-". Let's have a look at the following example:

// execute with "MyProgram.exe param1 /param2"

if FindCmdLineSwitch('param1') then
   ShowMessage('param1'); // will not be displayed

if FindCmdLineSwitch('param2') then
   ShowMessage('param2'); // will be displayed

if FindCmdLineSwitch('param3') then
   ShowMessage('param3'); // will not be displayed

Only with "param2", FindCmdLineSwitch will be true in this code, because "param1" do not start with "/" and "param3" does not appear in the list of parameters we have passed.

Other parameters of FindCmdLineSwitch are IgnoreCase and SwitchChars, but these are optional. IgnoreCase is true by default and ensures that lower and upper case letters in the parameters passed are not observed (true) or observed (false). SwitchCars allows to pass a TSysCharSet, which is a set of characters which then can also occur at the start of a parameter, so that it is found. For an example, we can look at this code:

procedure TestFindCmdLineSwitch();
var
  schars: TSysCharSet;
begin
  schars := ['/','.'];

  if FindCmdLineSwicth('param1') then begin
    // reacts to "/param1", "/PARAM1"
    // does not reacts to "param1", ".param1" 
  end;

  if FindCmdLineSwitch('param1', false) then begin
    // reacts to "/param1"
    // does not react to "param1", ".param1", "/PARAM1"
  end;

  if FindCmdLineSwitch('param1', schars, false) then begin
    // reacts to "/param1", ".param1"
    // does not react to "param1", "/PARAM1"
  end;
end;

Of course, you could also solve this by running completely through a loop with all the parameters, but as the example shows, FindCmdLineSwitch offers the shorter code.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.