NSIS-读取多个命令行参数

NSIS-读取多个命令行参数,nsis,Nsis,将单个参数传递给我的windows installer时 MyApplication.exe CLIENT="Your Mom" 我可以正确地阅读它,就像这样: ${GetParameters} $0 ${GetOptions} "$0" "CLIENT=" $CLIENT 但是一旦我尝试传递多个参数 MyApplication.exe CLIENT="Your Mom" NAME="Jill" LOCATION="Da Yard" TEL="0221456789" 试着这样读 ${Ge

将单个参数传递给我的windows installer时

MyApplication.exe CLIENT="Your Mom"
我可以正确地阅读它,就像这样:

${GetParameters} $0
${GetOptions} "$0" "CLIENT=" $CLIENT 
但是一旦我尝试传递多个参数

MyApplication.exe CLIENT="Your Mom" NAME="Jill" LOCATION="Da Yard" TEL="0221456789"
试着这样读

${GetParameters} $0
${GetOptions} "$0" "CLIENT=" $CLIENT 
${GetOptions} "$0" "NAME=" $NAME 
${GetOptions} "$0" "LOCATION=" $LOCATION 
${GetOptions} "$0" "TEL=" $TEL
$CLIENT
$NAME
$TEL
的值是正确的,但是
$LOCATION
将包含

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


我是否正确使用了
GetOptions

如果您更改所有参数,使其以
/
开头,那么出于某种原因,它会起作用!(MyApplication.exe/CLIENT=“Your Mom”/NAME=“Jill”/LOCATION=“Da Yard”/TEL=“0221456789”和
${GetOptions}“$0”“/LOCATION=“$LOCATION

如果您不能使用/switch前缀,那么您必须自己重写${GetOptions},或者等待下一个NSIS版本,希望在该版本中修复它

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

!include FileFunc.nsh

!macroundef GetOptionsBody

!macro GetOptionsBody _FILEFUNC_S
Exch $1 ; Prefix
Exch
Exch $0 ; String
Exch
ClearErrors

; Parse $0 here and look for $1 and store the suffix in $0...


FileFunc_GetOptions${_FILEFUNC_S}_notfound:
    SetErrors
    StrCpy $0 ''

FileFunc_GetOptions${_FILEFUNC_S}_end:

Pop $1
Exch $0
!macroend
编辑:

看起来这可能是出于设计,帮助文件中包含了一个小小的英文花絮:

第一个选项符号是分隔符

Edit2:

我从头开始做了一个新的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 $1 ; Prefix
Exch
Exch $0 ; String
Exch
Push $2 ; The quote type we are in if any (Currently only supports ")
Push $3 ; Position in $0
Push $4 ; Temp
Push $5 ; Temp
Push $6 ; Start of data
ClearErrors
StrCpy $2 ''
StrCpy $3 "-1"
StrCpy $6 "-1"
FileFunc_GetOptions${_FILEFUNC_S}_loop:
    StrCpy $5 $0 1 $3
    IntOp $3 $3 + 1
    StrCpy $4 $0 1 $3
    StrCmp $4 "" FileFunc_GetOptions${_FILEFUNC_S}_eos
    StrCmp $4 '"' FileFunc_GetOptions${_FILEFUNC_S}_foundquote
    StrCmp $2 '' 0 FileFunc_GetOptions${_FILEFUNC_S}_loop ; We are inside a quote, just keep looking for the end of it
    StrCmp -1 $6 0 FileFunc_GetOptions${_FILEFUNC_S}_dataisunquoted ; Have we already found the prefix and start of data?
    IntCmpU $3 0 +2 ; $3 starts as -1 so $5 might contain the last character so we force it to a space
    StrCmp $5 '$\t' 0 +2
    StrCpy $5 " "
    StrCmp $5 " " 0 FileFunc_GetOptions${_FILEFUNC_S}_loop ; The prefix must be at the start of the string or be prefixed by space or tab
    StrLen $4 $1
    StrCpy $5 $0 $4 $3
    StrCmp${_FILEFUNC_S} "$5" "$1" "" FileFunc_GetOptions${_FILEFUNC_S}_loop
    IntOp $6 $4 + $3 ; Data starts here
    IntOp $3 $6 - 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 $4 ' ' FileFunc_GetOptions${_FILEFUNC_S}_extractdata
    StrCmp $4 '$\t' FileFunc_GetOptions${_FILEFUNC_S}_extractdata FileFunc_GetOptions${_FILEFUNC_S}_loop
FileFunc_GetOptions${_FILEFUNC_S}_extractdata:
    IntOp $5 $3 - $6
    StrCpy $0 $0 $5 $6
    Goto FileFunc_GetOptions${_FILEFUNC_S}_return
FileFunc_GetOptions${_FILEFUNC_S}_foundquote:
    StrCmp $2 $4 FileFunc_GetOptions${_FILEFUNC_S}_endquote
    StrCpy $2 $4 ; Starting a quoted part
    Goto FileFunc_GetOptions${_FILEFUNC_S}_loop
FileFunc_GetOptions${_FILEFUNC_S}_endquote:
    StrCpy $2 ''
    StrCmp -1 $6 FileFunc_GetOptions${_FILEFUNC_S}_loop FileFunc_GetOptions${_FILEFUNC_S}_extractquoteddata
FileFunc_GetOptions${_FILEFUNC_S}_eos: ; End Of String
    StrCmp $2 '' +2
FileFunc_GetOptions${_FILEFUNC_S}_extractquoteddata:
    IntOp $6 $6 + 1 ; Skip starting quote when extracting the data
    StrCmp -1 $6 0 FileFunc_GetOptions${_FILEFUNC_S}_extractdata
    SetErrors
    StrCpy $0 ''
FileFunc_GetOptions${_FILEFUNC_S}_return:
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0
!macroend



Var CLIENT
Var NAME
Var LOCATION
Var TEL

Function Test

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

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

${GetOptions} $0 "CLIENT=" $CLIENT 
${GetOptions} $0 "NAME=" $NAME 
${GetOptions} $0 "LOCATION=" $LOCATION 
${GetOptions} $0 "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 $0 "%NSIS_Test_GetOptions%"
StrCmp $0 "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
Outfile“Test.exe”
RequestExecutionLevel用户
ShowInstDetails显示
!包括FileFunc.nsh

!如果${NSIS_PACKEDVERSION}我看到了相同的错误,我会调查并报告,如果我能想出一个解决方法…非常感谢,非常好的答案!最后使用了“/CLIENT=”,看起来是最干净的atm。仍然奇怪的是,前2个参数和最后一个参数被正确检索。我尝试了另一种解决方法,使用
StrStrip
在GetOptions检索到每个标志及其值后删除它们,但这是完美的!