我有两种可能的构建选项。由于我不希望我的客户使用某些参数启动安装程序,我最好将它们传递给编译器并在我的代码中完成所有工作。
假设我有变量 UNION
可能有两个值:0
和 1
.我必须在我的代码中分析该变量的值,并根据结果是否包含一些文件。
我知道如何将参数传递给安装程序本身,但如何将它们传递给编译器?
这是一些代码:
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
这些问题也涵盖了这一点:你可以在任何地方使用预处理器指令,甚至在
[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/