如何将带有值的命令行参数传递给Inno Setup Compiler,以便可以在代码中使用它们?
我有两个可能的构建选项.因为我不希望客户使用一些参数来启动安装程序,所以最好将它们传递给编译器,并完成代码中的所有工作.
I have two possible build options. As I don't want my clients to start the installer with some parameters, I'd better pass them to the compiler and do all the job in my code.
假设我有一个变量 UNION
,它可以采用两个值: 0
和 1
.我必须在代码中分析该变量的值,并根据结果是否包含某些文件来进行分析.我知道如何将参数传递给安装程序本身,但是如何将它们传递给编译器?
Let's say I have the variable UNION
which may take two values: 0
and 1
. I have to analyze the value of that variable in my code and depending on the result to include some files or not.
I know how to pass parameterrs to the installer itself, but how can I pass them to the compiler?
以下是一些代码:
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
是应该分析从命令行获取的参数,然后根据结果执行其功能的功能.
IsUnion
is the function that should analyze the parameter taken from the command line and then do its job depending on the result.
编译器(或从技术上讲预处理器)具有 /D
命令行开关,您可以用来设置预处理器变量.
Compiler (or technically the preprocessor) has /D
command-line switch, which you can use to set a preprocessor variable.
例如,这个...
ISCC.exe Example1.iss /DBinaryName=MyProg.exe
...具有相同的效果,就像您使用 #在脚本本身中定义
指令,如下所示:
... has the the same effect, as if you use #define
directive in the script itself, like this:
#define BinaryName "MyProg.exe"
因此您可以在脚本中以相同的方式使用它:
So you can use it the same way in the script:
[Files]
Source: "{#BinaryName}"; DestDir: "{app}"
您甚至可以在条件中使用变量,例如:
You can use a variable even in conditions like:
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
尽管要达到相同的效果,您只能使用变量存在,像:
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
以下问题也涵盖了这一点:
This is also covered in these questions:
您甚至可以在 [Code]
部分中的任何位置使用预处理器指令.
You can use the preprocessor directives anywhere, even in the [Code]
section.
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 ++预处理器.它并不关心(或非常关心)这些部分或代码结构.您甚至可以执行以下操作:
The preprocesor does not care, it kicks in as the very first step and treats the .iss
file as a plain text file. Pretty much like C/C++ preprocessor. It does not care (much) about the sections or code structure. You can even do things like:
DeleteFile(
ExpandConstant(
#ifdef DeleteFromUserData
'{userappdata}\MyProg'
#else
'{app}'
#endif
)+'\Locale\C4Union.UKR');
在脚本末尾添加 SaveToFile
以查看生成的代码