Is it possible to accept custom command line parameters with Inno Setup

Viewed 36006

I am preparing an installer with Inno Setup. But I'd like to add an additional custom (none of the available parameters) command line parameters and would like to get the value of the parameter, like:

setup.exe /do something

Check if /do is given, then get the value of something. Is it possible? How can I do this?

10 Answers

Inno Setup directly supports switches with syntax /Name=Value using {param} constant.


You can use the constant directly in sections, though this use is quite limited.

An example:

[Registry]
Root: HKCU; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; \
    ValueName: "Mode"; ValueData: "{param:Mode|DefaultMode}"

You will more likely want to use switches in Pascal Script.

If your switch has the syntax /Name=Value, the easiest way to read its value is using ExpandConstant function.

For example:

if ExpandConstant('{param:Mode|DefaultMode}') = 'DefaultMode' then
begin
  Log('Installing for default mode');
end
  else
begin
  Log('Installing for different mode');
end;

If you want to use a switch value to toggle entries in sections, you can use Check parameter and a auxiliary function, like:

[Files]
Source: "Client.exe"; DestDir: "{app}"; Check: SwitchHasValue('Mode', 'Client')
Source: "Server.exe"; DestDir: "{app}"; Check: SwitchHasValue('Mode', 'Server')
[Code]

function SwitchHasValue(Name: string; Value: string): Boolean;
begin
  Result := CompareText(ExpandConstant('{param:' + Name + '}'), Value) = 0;
end;

Ironically it is more difficult to check for a mere presence of switch (without a value).

Use can use a function CmdLineParamExists from @TLama's answer to Passing conditional parameter in Inno Setup.

function CmdLineParamExists(const Value: string): Boolean;
var
  I: Integer;  
begin
  Result := False;
  for I := 1 to ParamCount do
    if CompareText(ParamStr(I), Value) = 0 then
    begin
      Result := True;
      Exit;
    end;
end;

You can obviously use the function in Pascal Script:

if CmdLineParamExists('/DefaultMode') then
begin
  Log('Installing for default mode');
end
  else
begin
  Log('Installing for different mode');
end;

But you can even use it in sections, most typically using Check parameter:

[Files]
Source: "MyProg.hlp"; DestDir: "{app}"; Check: CmdLineParamExists('/InstallHelp')

A related problem:
Add user defined command line parameters to /? window

I've modified a little bit knguyen's answer. Now it's case insensitive (you can write en console /myParam or /MYPARAM) and it can accept default value. Also I fixed the case when you receive larger parameter then expected (for ex: /myParamOther="parameterValue" in place of /myParam="parameterValue". Now myParamOther doesn't match).

function GetCommandlineParam(inParamName: String; defaultParam: String): String; 
var
   paramNameAndValue: String;
   i: Integer;
begin
   Result := defaultParam;

   for i := 0 to ParamCount do
   begin
     paramNameAndValue := ParamStr(i);
     if  (Pos(Lowercase(inParamName)+'=', AnsiLowercase(paramNameAndValue)) = 1) then
     begin
       Result := Copy(paramNameAndValue, Length(inParamName)+2, Length(paramNameAndValue)-Length(inParamName));
       break;
     end;
   end;
end;
Related