command-line - 如何将带有值的命令行参数传递给 Inno Setup Compiler,以便我可以在我的代码中使用它们?

标签 command-line parameters inno-setup

我有两种可能的构建选项。由于我不希望我的客户使用某些参数启动安装程序,我最好将它们传递给编译器并在我的代码中完成所有工作。

假设我有变量 UNION可能有两个值:01 .我必须在我的代码中分析该变量的值,并根据结果是否包含一些文件。
我知道如何将参数传递给安装程序本身,但如何将它们传递给编译器?

这是一些代码:

procedure CurStepChanged(CurStep: TSetupStep);
var
  Code: Integer;
begin
  if CurStep = ssDone then
    begin
      if not IsUnion then
        begin
          DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.UKR');
          DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.ENU');  
        end;
    end;
end;
IsUnion是应该分析从命令行获取的参数然后根据结果执行其工作的函数。

最佳答案

编译器(或技术上的 preprocessor )有 /D command-line switch ,您可以使用它来设置预处理器变量。
例如这个...

ISCC.exe Example1.iss /DBinaryName=MyProg.exe
...具有相同的效果,就像您使用 #define directive 一样在脚本本身中,像这样:
#define BinaryName "MyProg.exe"
所以你可以在脚本中以同样的方式使用它:
[Files]
Source: "{#BinaryName}"; DestDir: "{app}"

您甚至可以在 conditions 中使用变量喜欢:
ISCC.exe Example1.iss /DMode=Install
#if Mode == "Install"
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
#elif Mode == "Delete"
[InstallDelete]
Type: files; Name: "{app}\MyProg.exe"
#else
#error Unknonn mode
#endif

虽然为了同样的效果,你可以只使用 variable existence , 喜欢:
ISCC.exe Example1.iss /DInstall /DDelete
#ifdef Install
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
#endif

#ifdef Delete
[InstallDelete]
Type: files; Name: "{app}\MyProg.exe"
#endif
这些问题也涵盖了这一点:
  • How do I build two different installers from the same script in Inno Setup?
  • Compile Inno Setup installer for specific component only

  • 你可以在任何地方使用预处理器指令,甚至在 [Code] 中部分。
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssDone then
      begin
        #ifdef Delete
        DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.UKR');
        DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.ENU');  
        #endif
      end;
    end;
    
    甚至:
    #ifdef Delete
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssDone then
      begin
        DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.UKR');
        DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.ENU');  
      end;
    end;
    #endif
    
    预处理器不在乎,它作为第一步开始处理 .iss文件作为纯文本文件。很像C/C++ preprocessor .它不关心(很多)部分或代码结构。您甚至可以执行以下操作:
    DeleteFile(
      ExpandConstant(
        #ifdef DeleteFromUserData
        '{userappdata}\MyProg'
        #else
        '{app}'
        #endif
        )+'\Locale\C4Union.UKR');
    

    Add SaveToFile to the end of the script查看生成的代码。

    关于command-line - 如何将带有值的命令行参数传递给 Inno Setup Compiler,以便我可以在我的代码中使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60888905/

    相关文章:

    c# - 从被调用线程获取信息返回主线程?

    windows - Inno 安装程序 : Use "Program Files" directory on both 32bit/64bit systems with {pf}

    inno-setup - Inno Setup - 如何防止它询问用户是否要创建桌面快捷方式

    node.js - 如何在 Node.js 中打开命令行窗口?

    shell - 在 ZSH 中声明数组

    java - 在 Windows 上通过命令行设置 JVM

    inno-setup - 在 Inno Setup 中更改任务列表框和其他控件的背景颜色

    Unix 排序仅对一列进行排序

    sql - Slick - 参数在查询模板中被忽略

    c# - 未提供的 SqlCommand.Parameters.AddWithValue 问题 : Procedure or function X expects parameter @Y,