Inno setup 将包含引号的命令行参数传递给安装程序

Inno setup 将包含引号的命令行参数传递给安装程序,inno-setup,Inno Setup,我正在尝试将自定义命令行参数传递给使用Inno Setup创建的安装程序。参数值实际上由几个参数组成,这些参数将用于在安装完成时启动已安装的程序,因此该值包含空格和引号,以便将参数组合在一起 例如,当-arg“C:\path with spaces”-moreargs应用作[Run]部分条目中的参数时,我希望按如下方式启动安装程序: setup.exe /abc="-arg "C:\path with spaces" -moreargs" 通过ParamStr()输出安装程序在[code]部分

我正在尝试将自定义命令行参数传递给使用Inno Setup创建的安装程序。参数值实际上由几个参数组成,这些参数将用于在安装完成时启动已安装的程序,因此该值包含空格和引号,以便将参数组合在一起

例如,当
-arg“C:\path with spaces”-moreargs
应用作
[Run]
部分条目中的
参数时,我希望按如下方式启动安装程序:

setup.exe /abc="-arg "C:\path with spaces" -moreargs"
通过
ParamStr()
输出安装程序在
[code]
部分接收到的参数(当然)会显示它们被拆分:
/abc=-arg C:\path
with
空格-moreargs

我如何避开引号以保留它们

我尝试将内部引号加倍:

setup.exe /abc="-arg ""C:\path with spaces"" -moreargs"
这可以正确地将参数保存在一起(
/abc=-arg C:\path with spaces-moreargs
),但是似乎
ParamStr()
删除了所有引号

有没有办法在使用
ParamStr()
或param常量
{param:abc | DefaultValue}
检索的参数中保留引号


替代方法似乎是从
GetCmdTail
(包含原始参数字符串)执行我自己的参数解析,或者使用另一个字符而不是
ParamStr()
中保留的内部引号,然后用引号替换它们。但是,如果有办法使用内置函数,我宁愿不这样做。

似乎
{param}
ParamStr()
都去掉了双引号,但是正如您指出的那样(谢谢!)
GetCmdTail
函数返回了原始值

下面是一个函数,用于获取带引号的原始参数:

function ParamStrWithQuotes(ParamName: String) : string;
var
  fullCmd : String;
  currentParamName : string;
  i : Integer;
  startPos : Integer;
  endPos : Integer;
begin

  fullCmd := GetCmdTail
  // default to end of string, in case the option is the last item
  endPos := Length(fullCmd);

  for i := 0 to ParamCount-1 do
  begin
    // extract parameter name (eg, "/Option=")
    currentParamName := Copy(ParamStr(i), 0, pos('=',ParamStr(i)));

    // once found, we want the following item
    if (startPos > 0) then 
    begin 
      endPos := pos(currentParamName,fullCmd)-2; // -1 to move back to actual end position, -1 for space
      break; // exit loop
    end;

    if (CompareText(currentParamName, '/'+ParamName+'=') = 0) then // case-insensitive compare 
    begin
      // found target item, so save its string position
      StartPos := pos(currentParamName,fullCmd)+2+Length(ParamName);
    end;
  end;

  if ((fullCmd[StartPos] = fullCmd[EndPos])
     and ((fullCmd[StartPos] = '"') or (fullCmd[StartPos] = ''''))) then
  begin
    // exclude surrounding quotes
    Result := Copy(fullCmd, StartPos+1, EndPos-StartPos-1);
  end
  else 
  begin
    // return as-is
    Result := Copy(fullCmd, StartPos, EndPos-StartPos+1);
  end;
end;
您可以使用
{code:ParamStrWithQuotes | abc}

调用setup.exe时,您必须转义引号,因此以下操作之一可以实现此目的:

setup.exe /abc="-arg ""C:\path with spaces"" -moreargs"


事实上,
ParamStr
是愚蠢的,所以你必须选择一种替代方案。或者改变你的逻辑。将参数分解为几个部分。然后将它们放回一起进行函数调用。这并不是说它对您有帮助,但我希望在传入命令行参数时支持JSON字符串。
setup.exe /abc='-arg "C:\path with spaces" -moreargs'