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

时间:2010-09-01 13:02:07

标签: command-line inno-setup command-line-arguments

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

setup.exe /do something

检查是否给出/do,然后获取某物的值。可能吗?我怎么能这样做?

10 个答案:

答案 0 :(得分:29)

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

c:\> myAppInstaller.exe /foo=wiggle

并在你的myApp.iss中:

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

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

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

答案 1 :(得分: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;

答案 2 :(得分: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;

答案 3 :(得分: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;

答案 4 :(得分:7)

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

另一种可能性是使用GetCmdTail

答案 5 :(得分:5)

Inno Setup使用{param} constant直接支持语法为/Name=Value的交换机。

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

一个例子:

[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对Passing conditional parameter in Inno Setup

的回答的函数CmdLineParamExists
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')

答案 6 :(得分:0)

我找到了答案:GetCmdTail。

答案 7 :(得分:0)

回应:

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

  • 事实并非如此。尝试在InnoSetup 5.5.6中的ExpandConstant中使用命令行参数会导致运行时错误。
PS:我会直接添加评论,但显然我没有足够的“声誉”

答案 8 :(得分:0)

我已经修改了一点 knguyen 的答案。现在它不区分大小写(您可以编写en console / myParam或/ MYPARAM)并且它可以接受默认值。此外,当你收到更大的参数然后预期时我修复了这个案例(例如:/ myParamOther =&#34; parameterValue&#34;代替/ myParam =&#34; parameterValue&#34;。现在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;

答案 9 :(得分:-1)

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