是否可以使用Inno Setup接受自定义命令行参数

Sim*_*lar 28 command-line inno-setup command-line-arguments

我正在准备Inno Setup的安装程序.但我想添加一个额外的自定义(没有可用的参数)命令行参数,并希望获得参数的值,如:

setup.exe /do something
Run Code Online (Sandbox Code Playgroud)

检查是否/do给出,然后获取某事物的价值.可能吗?我怎样才能做到这一点?

Dan*_*cks 31

使用InnoSetup 5.5.5(可能还有其他版本),只需将您想要的任何内容作为参数传递,前缀为a /

c:\> myAppInstaller.exe /foo=wiggle
Run Code Online (Sandbox Code Playgroud)

在你的myApp.iss中:

[Setup]
AppName = {param:foo|waggle}
Run Code Online (Sandbox Code Playgroud)

|waggle如果没有参数匹配提供一个默认值.Inno设置不区分大小写.这是处理命令行选项的一种特别好的方法:它们刚刚存在.我希望有一种方法让用户知道安装程序关心的命令行参数.

顺便说一句,这使得@ knguyen和@ steve-dunn的答案有些多余.实用程序函数完全执行内置的{param:}语法.


Mar*_*rry 12

继@DanLocks的回答之后,常量页面底部附近记录了{param:ParamName | DefaultValue }常量:

http://www.jrsoftware.org/ishelp/index.php?topic=consts

我发现可选地抑制许可页面非常方便.这是我需要添加的所有内容(使用Inno Setup 5.5.6(a)):

[code]
{ If there is a command-line parameter "skiplicense=true", don't display license page }
function ShouldSkipPage(PageID: Integer): Boolean;
begin
  Result := False
  if PageId = wpLicense then
    if ExpandConstant('{param:skiplicense|false}') = 'true' then
      Result := True;
end;
Run Code Online (Sandbox Code Playgroud)


小智 10

如果要从inno中的代码解析命令行参数,请使用与此类似的方法.只需从命令行调用inno脚本,如下所示:

c:\MyInstallDirectory>MyInnoSetup.exe -myParam parameterValue
Run Code Online (Sandbox Code Playgroud)

然后你可以在任何需要的地方调用GetCommandLineParam:

myVariable := GetCommandLineParam('-myParam');
Run Code Online (Sandbox Code Playgroud)
{ ================================================================== }
{ Allows for standard command line parsing assuming a key/value organization }
function GetCommandlineParam (inParam: String):String;
var
  LoopVar : Integer;
  BreakLoop : Boolean;
begin
  { Init the variable to known values }
  LoopVar :=0;
  Result := '';
  BreakLoop := False;

  { Loop through the passed in arry to find the parameter }
  while ( (LoopVar < ParamCount) and
          (not BreakLoop) ) do
  begin
    { Determine if the looked for parameter is the next value }
    if ( (ParamStr(LoopVar) = inParam) and
         ( (LoopVar+1) <= ParamCount )) then
    begin
      { Set the return result equal to the next command line parameter }
      Result := ParamStr(LoopVar+1);

      { Break the loop }
      BreakLoop := True;
    end;

    { Increment the loop variable }
    LoopVar := LoopVar + 1;
  end;
end;
Run Code Online (Sandbox Code Playgroud)


kng*_*yen 10

这是我写的功能,这是Steven Dunn答案的改进.您可以将其用作:

c:\MyInstallDirectory>MyInnoSetup.exe /myParam="parameterValue"
Run Code Online (Sandbox Code Playgroud)
myVariable := GetCommandLineParam('/myParam');
Run Code Online (Sandbox Code Playgroud)
{ 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;
Run Code Online (Sandbox Code Playgroud)

  • 这些答案很好,但是对于多个参数,确定“ParamCount”是另一个考虑因素。这就是为什么`ExpandConstant` 更容易。 (2认同)

Oth*_*ide 7

是的,您可以使用ParamStrPascalScript中的函数来访问所有命令行参数.该ParamCount函数将为您提供命令行参数的数量.

另一种可能性是使用 GetCmdTail


Mar*_*ryl 7

Inno Setup直接支持/Name=Value使用{param}常量语法的开关.


您可以直接在部分中使用常量,但这种用途非常有限.

一个例子:

[Registry]
Root: HKCU; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; \
    ValueName: "Mode"; ValueData: "{param:Mode|DefaultMode}"
Run Code Online (Sandbox Code Playgroud)

您更可能希望在Pascal脚本中使用开关.

如果您的开关具有语法/Name=Value,则读取其值的最简单方法是使用ExpandConstant函数.

例如:

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

如果要使用切换值切换节中的条目,可以使用Check参数和辅助功能,如:

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

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

具有讽刺意味的是,检查交换机的存在(没有价值)更加困难.

使用可以使用CmdLineParamExists@ TLama 在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;
Run Code Online (Sandbox Code Playgroud)

您显然可以在Pascal脚本中使用该函数:

if CmdLineParamExists('/DefaultMode') then
begin
  Log('Installing for default mode');
end
  else
begin
  Log('Installing for different mode');
end;
Run Code Online (Sandbox Code Playgroud)

但您甚至可以在部分中使用它,通常使用Check参数:

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

  • 一个很棒的答案,正是我所需要的。感谢您花时间为各种场景编写所有有用的代码片段! (2认同)

Sim*_*lar -1

我找到了答案:GetCmdTail。