NSIS - 读取多个命令行参数

NSIS - Reading multiple command line arguments

将单个参数传递给我的 windows 安装程序时,

MyApplication.exe CLIENT="Your Mom"

我可以使用 GetParameters and GetOptions 正确阅读它,就像这样:

${GetParameters} [=11=]
${GetOptions} "[=11=]" "CLIENT=" $CLIENT 

但是一旦我尝试传递多个参数

MyApplication.exe CLIENT="Your Mom" NAME="Jill" LOCATION="Da Yard" TEL="0221456789"

并尝试像这样阅读它

${GetParameters} [=13=]
${GetOptions} "[=13=]" "CLIENT=" $CLIENT 
${GetOptions} "[=13=]" "NAME=" $NAME 
${GetOptions} "[=13=]" "LOCATION=" $LOCATION 
${GetOptions} "[=13=]" "TEL=" $TEL

$CLIENT$NAME$TEL 的值是正确的,但 $LOCATION 将包含

Da Yard TEL=0221456789

如果我添加更多参数,也会发生同样的情况,前两个和最后一个总是正确的,但中间的参数总是包含传递给安装程序的字符串的一些子字符串。

我使用 GetOptions 正确吗?

如果您更改所有参数,使它们以 / 开头,那么出于某种原因它可以工作! (MyApplication.exe /CLIENT="Your Mom" /NAME="Jill" /LOCATION="Da Yard" /TEL="0221456789" and ${GetOptions} "[=13 =]" "/LOCATION=" $LOCATION)

如果您无法忍受 / 开关前缀,那么您将不得不自己重写 ${GetOptions} 或等到下一个 NSIS 版本有望修复它。

如果您想尝试自己修复它,那么它看起来像这样:

!include FileFunc.nsh

!macroundef GetOptionsBody

!macro GetOptionsBody _FILEFUNC_S
Exch  ; Prefix
Exch
Exch [=10=] ; String
Exch
ClearErrors

; Parse [=10=] here and look for  and store the suffix in [=10=]...


FileFunc_GetOptions${_FILEFUNC_S}_notfound:
    SetErrors
    StrCpy [=10=] ''

FileFunc_GetOptions${_FILEFUNC_S}_end:

Pop 
Exch [=10=]
!macroend

编辑:

这似乎是设计使然,帮助文件包含这个破烂的英文花絮:

First option symbol it is delimiter

编辑2:

我从头开始做了一个新的GetOptions,它只支持双引号,而且可能在各方面都有所不同。我没有测试太多,但似乎工作正常:

Outfile "Test.exe"
RequestExecutionLevel user
ShowInstDetails show

!include FileFunc.nsh

!if '${NSIS_PACKEDVERSION}' <= 0x0300003f ; Older versions don't support !macroundef
!define GetOptionsBody_Alt GetOptionsBody_Alt
!undef GetOptions
!define GetOptions `!insertmacro GetOptionsCall_Alt`
!macro GetOptionsCall_Alt _PARAMETERS _OPTION _RESULT
    !verbose push
    !verbose ${_FILEFUNC_VERBOSE}
    Push `${_PARAMETERS}`
    Push `${_OPTION}`
    ${CallArtificialFunction} GetOptions_Alt_
    Pop ${_RESULT}
    !verbose pop
!macroend
!macro GetOptions_Alt_
    !verbose push
    !verbose ${_FILEFUNC_VERBOSE}
    !insertmacro ${GetOptionsBody_Alt} ''
    !verbose pop
!macroend
!else
!define GetOptionsBody_Alt GetOptionsBody
!macroundef ${GetOptionsBody_Alt}
!endif

!macro ${GetOptionsBody_Alt} _FILEFUNC_S ; This alternative version only knows about " quotes and assumes there is nothing or a space/tab before the prefix 
Exch  ; Prefix
Exch
Exch [=11=] ; String
Exch
Push  ; The quote type we are in if any (Currently only supports ")
Push  ; Position in [=11=]
Push  ; Temp
Push  ; Temp
Push  ; Start of data
ClearErrors
StrCpy  ''
StrCpy  "-1"
StrCpy  "-1"
FileFunc_GetOptions${_FILEFUNC_S}_loop:
    StrCpy  [=11=] 1 
    IntOp   + 1
    StrCpy  [=11=] 1 
    StrCmp  "" FileFunc_GetOptions${_FILEFUNC_S}_eos
    StrCmp  '"' FileFunc_GetOptions${_FILEFUNC_S}_foundquote
    StrCmp  '' 0 FileFunc_GetOptions${_FILEFUNC_S}_loop ; We are inside a quote, just keep looking for the end of it
    StrCmp -1  0 FileFunc_GetOptions${_FILEFUNC_S}_dataisunquoted ; Have we already found the prefix and start of data?
    IntCmpU  0 +2 ;  starts as -1 so  might contain the last character so we force it to a space
    StrCmp  '$\t' 0 +2
    StrCpy  " "
    StrCmp  " " 0 FileFunc_GetOptions${_FILEFUNC_S}_loop ; The prefix must be at the start of the string or be prefixed by space or tab
    StrLen  
    StrCpy  [=11=]  
    StrCmp${_FILEFUNC_S} "" "" "" FileFunc_GetOptions${_FILEFUNC_S}_loop
    IntOp   +  ; Data starts here
    IntOp   - 1 ; This is just to ignore the + 1 at the top of the loop
    Goto FileFunc_GetOptions${_FILEFUNC_S}_loop
FileFunc_GetOptions${_FILEFUNC_S}_dataisunquoted:
    StrCmp  ' ' FileFunc_GetOptions${_FILEFUNC_S}_extractdata
    StrCmp  '$\t' FileFunc_GetOptions${_FILEFUNC_S}_extractdata FileFunc_GetOptions${_FILEFUNC_S}_loop
FileFunc_GetOptions${_FILEFUNC_S}_extractdata:
    IntOp   - 
    StrCpy [=11=] [=11=]  
    Goto FileFunc_GetOptions${_FILEFUNC_S}_return
FileFunc_GetOptions${_FILEFUNC_S}_foundquote:
    StrCmp   FileFunc_GetOptions${_FILEFUNC_S}_endquote
    StrCpy   ; Starting a quoted part
    Goto FileFunc_GetOptions${_FILEFUNC_S}_loop
FileFunc_GetOptions${_FILEFUNC_S}_endquote:
    StrCpy  ''
    StrCmp -1  FileFunc_GetOptions${_FILEFUNC_S}_loop FileFunc_GetOptions${_FILEFUNC_S}_extractquoteddata
FileFunc_GetOptions${_FILEFUNC_S}_eos: ; End Of String
    StrCmp  '' +2
FileFunc_GetOptions${_FILEFUNC_S}_extractquoteddata:
    IntOp   + 1 ; Skip starting quote when extracting the data
    StrCmp -1  0 FileFunc_GetOptions${_FILEFUNC_S}_extractdata
    SetErrors
    StrCpy [=11=] ''
FileFunc_GetOptions${_FILEFUNC_S}_return:
Pop 
Pop 
Pop 
Pop 
Pop 
Pop 
Exch [=11=]
!macroend



Var CLIENT
Var NAME
Var LOCATION
Var TEL

Function Test

${GetParameters} [=11=]
DetailPrint "Calling GetOptions on |[=11=]|"
${GetOptions} [=11=] "/CLIENT=" $CLIENT 
${GetOptions} [=11=] "/NAME=" $NAME 
${GetOptions} [=11=] "/LOCATION=" $LOCATION 
${GetOptions} [=11=] "/TEL=" $TEL

DetailPrint "/CLIENT=|$CLIENT|"
DetailPrint "/NAME=|$NAME|"
DetailPrint "/LOCATION=|$LOCATION|"
DetailPrint "/TEL=|$TEL|"

${GetOptions} [=11=] "CLIENT=" $CLIENT 
${GetOptions} [=11=] "NAME=" $NAME 
${GetOptions} [=11=] "LOCATION=" $LOCATION 
${GetOptions} [=11=] "TEL=" $TEL

DetailPrint "CLIENT=|$CLIENT|"
DetailPrint "NAME=|$NAME|"
DetailPrint "LOCATION=|$LOCATION|"
DetailPrint "TEL=|$TEL|"

FunctionEnd


Section

; Normally I would detect this automation with a command line parameter but 
; because we are debugging those I'm using this hack instead
ExpandEnvStrings [=11=] "%NSIS_Test_GetOptions%"
StrCmp [=11=] "1337" 0 launchselfwithparams
    Call Test
Goto done
launchselfwithparams:
    System::Call 'KERNEL32::SetEnvironmentVariable(t "NSIS_Test_GetOptions", t "1337")'
    DetailPrint "NSIS ${NSIS_VERSION}"
    ExecWait '"$ExePath" /CLIENT="Your Mom"'
    ExecWait '"$ExePath" /CLIENT="Your Mom" /NAME="Jill" /LOCATION="Da Yard" /TEL="0221456789"'

    ExecWait '"$ExePath" CLIENT="Your Mom"'
    ExecWait '"$ExePath" CLIENT="Your Mom" NAME="Jill" LOCATION="Da Yard" TEL="0221456789"'

    ${GetOptions} 'foo=bar bar=baz baz=biz' 'bar=' $R0
    DetailPrint |$R0|
    ${GetOptions} 'foo=bar bar=baz baz=biz' 'failthis=' $R0
    DetailPrint |$R0|

    ${GetOptions} '/SILENT=yes/INSTDIR=bug /INSTDIR="C:/Program Files/Common Files" /ADMIN=password' "/INSTDIR="  $R0
    DetailPrint |$R0|
    ${GetOptions} 'SILENT=yesINSTDIR=bug INSTDIR="C:/Program Files/Common Files" ADMIN=password' "INSTDIR="  $R0
    DetailPrint |$R0|

    ${GetOptions} '/SILENT="yes/INSTDIR=bug" /INSTDIR="C:/Program Files/Common Files" /ADMIN="password"' "/INSTDIR="  $R0
    DetailPrint |$R0|
    ${GetOptions} 'SILENT="yesINSTDIR=bug" INSTDIR="C:/Program Files/Common Files" ADMIN="password"' "INSTDIR="  $R0
    DetailPrint |$R0|
done:

SectionEnd