How to pass custom command-line parameters in InnoSetup

Posted on January 17, 2021 at 6:30 pm

You can read custom parameters with the {param} constant, example:

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

It works with syntax like /Name=Value.

And can also be used within sections, such as:

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

In sections you can also use Check parameter and a custom function:

[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;

If you just want to check if a parameter just exist, use this:

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;

This function can be used in Pascal scripting [Code] section:

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

And also in sections:

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

Alternatively you may use the GetCmdTail() function:

if Pos('/NOICONS', Uppercase(GetCmdTail())) > 0 then
begin
  Log('Param /NOICONS found');
end;

References:

https://stackoverflow.com/a/48349992
https://stackoverflow.com/a/14415103
https://jrsoftware.org/ishelp/index.php?topic=isxfunc_comparepackedversion

Updated on February 6, 2021 at 4:22 pm

Receive updates via email

Other Posts

Updated Posts