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

发布于 2024-09-16 12:14:30 字数 177 浏览 4 评论 0原文

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

setup.exe /do something

检查是否给出了 /do ,然后获取某物的价值。是否可以?我该怎么做?

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 value of the parameter, like:

setup.exe /do something

Check if /do is given, then get the value of something. Is it possible? How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

巷雨优美回忆 2024-09-23 12:14:30

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

c:\> myAppInstaller.exe /foo=wiggle

并在 myApp.iss 中:

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

|waggle 提供如果没有参数匹配则使用默认值。 Inno 设置不区分大小写。这是处理命令行选项的一种特别好的方法:它们突然出现。我希望有一种巧妙的方法让用户知道安装程序关心哪些命令行参数。

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

With InnoSetup 5.5.5 (and perhaps other versions), just pass whatever you want as a parameter, prefixed by a /

c:\> myAppInstaller.exe /foo=wiggle

and in your myApp.iss:

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

The |waggle provides a default value if no parameter matches. Inno setup is not case sensitive. This is a particularly nice way to handle command line options: They just spring into existence. I wish there was as slick a way to let users know what command line parameters the installer cares about.

BTW, this makes both @knguyen's and @steve-dunn's answers somewhat redundant. The utility functions do exactly what the built-in {param: } syntax does.

离去的眼神 2024-09-23 12:14:30

Inno Setup 直接支持使用 {param} 常量


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

示例:

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

您更有可能希望在 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;

如果您想使用开关值来切换部分中的条目,您可以使用 < code>Check 参数 和辅助函数,例如:

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

具有讽刺意味的是,仅检查 switch 是否存在(没有值)会更困难。

用户可以使用@TLama对在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 Script 中使用该函数:

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

但您甚至可以在部分中使用它,最典型的是使用 检查参数

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

相关问题:
将用户定义的命令行参数添加到 /?窗口

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

不交电费瞎发啥光 2024-09-23 12:14:30

根据 @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;

Further to @DanLocks' answer, the {param:*ParamName|DefaultValue*} constant is documented near the bottom of the Constants page:

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

I found it quite handy for optionally suppressing the license page. Here is all I needed to add (using 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;
滥情稳全场 2024-09-23 12:14:30

如果您想从 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;

If you want to parse command line arguments from code in inno, then use a method similar to this. Just call the inno script from the command line as follows:

c:\MyInstallDirectory>MyInnoSetup.exe -myParam parameterValue

Then you can call the GetCommandLineParam like this wherever you need it:

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;
哎呦我呸! 2024-09-23 12:14:30

这是我写的函数,是对 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;

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;
痴意少年 2024-09-23 12:14:30

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

另一种可能性是使用 GetCmdTail

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

说好的呢 2024-09-23 12:14:30

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

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

浴红衣 2024-09-23 12:14:30

响应:

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

  • 事实并非如此。尝试在 InnoSetup 5.5.6 中的 ExpandConstant 中使用命令行参数会导致运行时错误。

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

In response to:

"With InnoSetup 5.5.5 (and perhaps other versions), just pass whatever you want as a parameter, prefixed by a /"
"@NickG, yes, every constant you can expand by the ExpandConstant function"

  • This is not the case. Trying to use a command line parameter in ExpandConstant in InnoSetup 5.5.6 results in a runtime error.

PS: I would have added a comment directly but apparently I dont have enough "reputation"

一抹淡然 2024-09-23 12:14:30

我对knguyen的答案做了一些修改。现在它不区分大小写(您可以编写控制台 /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;

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;
你好,陌生人 2024-09-23 12:14:30

我找到了答案:GetCmdTail。

I found the answer: GetCmdTail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文