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

问题描述 投票:25回答:10

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

setup.exe /do something

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

command-line inno-setup command-line-arguments
10个回答
28
投票

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

c:\> myAppInstaller.exe /foo=wiggle

在你的myApp.iss中:

[Setup]
AppName = {param:foo|waggle}

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

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


-1
投票

您可以将参数传递给安装程序脚本。安装Inno Setup Preprocessor并阅读有关传递自定义命令行参数的文档。


11
投票

继@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;

9
投票

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

c:\MyInstallDirectory>MyInnoSetup.exe -myParam parameterValue

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

myVariable := GetCommandLineParam('-myParam');
{ ================================================================== }
{ 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;

9
投票

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

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;

7
投票

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

另一种可能性是使用GetCmdTail


5
投票

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


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

一个例子:

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

你更可能想在Pascal Script中使用开关。

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

例如:

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

如果要使用切换值来切换节中的条目,可以使用Check parameter和辅助函数,如:

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

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

使用可以使用来自@ TLama对CmdLineParamExists的回答的函数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;

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

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

但你甚至可以在部分中使用它,最常用的是使用Check参数:

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

0
投票

我找到了答案:GetCmdTail。


0
投票

回应:

“使用InnoSetup 5.5.5(可能还有其他版本),只需传递任何你想要的参数,前缀为/”“@NickG,是的,你可以通过ExpandConstant函数扩展每个常量”

  • 不是这种情况。尝试在InnoSetup 5.5.6中的ExpandConstant中使用命令行参数会导致运行时错误。

PS:我会直接添加评论,但显然我没有足够的“声誉”


0
投票

我修改了一点knguyen的答案。现在它不区分大小写(您可以编写en console / myParam或/ MYPARAM)并且它可以接受默认值。此外,当你收到更大的参数然后预期时,我修复了这个案例(例如:/ myParamOther =“parameterValue”代替/ myParam =“parameterValue”。现在myParamOther不匹配)。

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;
© www.soinside.com 2019 - 2024. All rights reserved.