2010-09-01 137 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') 
相關問題