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

前端 未结 10 1135
梦谈多话
梦谈多话 2020-12-02 15:33

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 v

相关标签:
10条回答
  • 2020-12-02 16:05

    You can pass parameters to your installer scripts. Install the Inno Setup Preprocessor and read the documentation on passing custom command-line parameters.

    0 讨论(0)
  • 2020-12-02 16:11

    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

    0 讨论(0)
  • 2020-12-02 16:18

    This is the function I wrote, which is an improvement of Steven Dunn's answer. You can use it as:

    c:\MyInstallDirectory>MyInnoSetup.exe /myParam="parameterValue"
    
    myVariable := GetCommandLineParam('/myParam');
    
    { util method, equivalent to C# string.StartsWith }
    function StartsWith(SubStr, S: String): Boolean;
    begin
      Result:= Pos(SubStr, S) = 1;
    end;
    
    { util method, equivalent to C# string.Replace }
    function StringReplace(S, oldSubString, newSubString: String): String;
    var
      stringCopy: String;
    begin
      stringCopy := S; { Prevent modification to the original string }
      StringChange(stringCopy, oldSubString, newSubString);
      Result := stringCopy;
    end;
    
    { ================================================================== }
    function GetCommandlineParam(inParamName: String): String; 
    var
       paramNameAndValue: String;
       i: Integer;
    begin
       Result := '';
    
       for i := 0 to ParamCount do
       begin
         paramNameAndValue := ParamStr(i);
         if (StartsWith(inParamName, paramNameAndValue)) then
         begin
           Result := StringReplace(paramNameAndValue, inParamName + '=', '');
           break;
         end;
       end;
    end;
    
    0 讨论(0)
  • 2020-12-02 16:20

    I found the answer: GetCmdTail.

    0 讨论(0)
  • 2020-12-02 16:21

    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;
    
    0 讨论(0)
  • 2020-12-02 16:22

    Yes it is possible, you can use the ParamStr function in PascalScript to access all the commandline parameters. The ParamCount function will give you the number of commandline parameters.

    Another possibility is to use GetCmdTail

    0 讨论(0)
提交回复
热议问题