2010-09-01 136 views
21

我正在准备安装Inno Setup的安装程序。但我想添加一个额外的自定义(没有可用参数)命令行参数,并想获得的参数的值,如:是否可以通过Inno Setup接受自定义命令行参数

setup.exe /do something 

检查/do给出,那么获得的价值东西。可能吗?我怎样才能做到这一点?

回答

0

我找到了答案:GetCmdTail。

7

是的,可以使用PascalScript中的ParamStr函数来访问所有的命令行参数。 ParamCount函数会给你一些命令行参数。

另一种可能性是使用GetCmdTail

-1

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

+1

在编译安装程序之前处理预处理器代码,因此无法使用它来检查生成的setup.exe的命令行参数。 – Otherside 2010-09-01 13:25:05

+0

我知道,这就是我指定“安装程序脚本”而不是编译的安装程序可执行文件的原因。我经常需要这样做,所以我想我会提到这种可能性。 – Bernard 2010-09-01 13:40:40

8

如果你想解析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; 
+0

我发现这非常有用,但我不认为它支持引用的参数值。 – 2013-12-02 12:42:33

+1

@knguyen的回答非常简单,适用于引用的值。 – 2013-12-02 13:16:15

+0

您可以使用'ExpandConstant'来实现单行代码。看到我的答案:https://stackoverflow.com/a/48349992/850848 – 2018-01-19 21:49:37

9

这是函数我写道,这是史蒂文邓恩的回答的改进。你可以使用它作为:

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; 
+1

这很好。我只添加了代码来自动追加一个'/'到inParamName值,如果它尚未开始。这样我只需要指定参数名称,并且在获取参数值时不必担心正确的参数名称前缀。 – 2013-12-02 13:13:17

+1

这些答案很好,但有了多个参数,确定'ParamCount'是另一个考虑因素。这就是为什么'ExpandConstant'更容易。 – 2018-01-19 13:09:11

+0

正如@LaurieStearn正确评论的那样,您可以使用'ExpandConstant'来实现单行代码。看到我的回答:https://stackoverflow.com/a/48349992/850848 – 2018-01-19 21:49:21

21

用InnoSetup 5.5.5(也许还有其他版本),只是通过任何你想作为一个参数,并用/

前缀
c:\> myAppInstaller.exe /foo=wiggle 

and in your myApp.iss:

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

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

顺便说一下,这使得@ knguyen's和@ steve-dunn的答案有点多余。实用函数完全按照内置的{param:}语法进行操作。

+1

@TLama:你说得对。我已经将预处理器选项与设置选项合并在我的脑海中。 – 2015-02-09 23:03:06

+0

有没有办法在代码中利用这种机制?我只能使它在[Setup]部分中工作,但是你能否以某种方式在[Script]部分中使用它? – NickG 2015-02-11 12:22:51

+1

@NickG,是的,你可以通过'ExpandConstant'函数扩展每个常量。 – TLama 2015-02-12 09:49:48

0

作为回应:

“With InnoSetup 5.5。5(也许还有其他版本),只是通过任何你想作为一个参数,并用/” ‘@NickG前缀,是的,每一个常数,你可以通过ExpandConstant功能’

  • 扩大这是不是这样的。试图在InnoSetup运行时错误5.5.6结果使用命令行参数ExpandConstant

PS:我会直接添加评论,但显然我没有足够的“信誉”

+0

适用于5.5.6(a)中的我。虽然常量的语法很奇怪,您必须用ExpandConstant函数调用中的单引号括住它们。以我的答案为例。 – 2016-01-30 00:27:07

7

进一步to @DanLocks'的答案,{参数:PARAMNAME |默认值}常数记录接近常数页面底部:

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

我发现它的可选抑制许可证页面很方便。这是所有我需要添加(使用Inno Setup的5.5.6(一)):

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

我已经修改了一点点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; 
+0

马丁,非常感谢。它确实有效,但是使用这个语法ExpandConstant('{param:name | defaultvalue}') – 2018-01-16 14:07:34

+0

好的,是的,所以修复我的命令:\t 虽然这会起作用,但这完全没有必要,因为其他答案显示可以使用' ExpandConstant('{param:name | defaultvalue}')'实现完全相同的功能。 – 2018-01-16 14:10:49

1

创新安装使用{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; 

具有讽刺意味的是它更多是二很难检查是否存在开关(没有值)。

使用可以使用的功能CmdLineParamExists从@ TLama的回答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; 

可以很明显的使用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') 
相关问题