将单个参数传递给我的Windows安装程序时,
MyApplication.exe CLIENT="Your Mom"
我可以使用GetParameters和GetOptions正确阅读,如下所示:
${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
吗?
答案 0 :(得分:1)
如果您更改了所有参数,以便它们以/
开头,那么它可以正常运行! ( MyApplication.exe / CLIENT =“你的妈妈”/ 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
修改:
似乎这可能是设计上的,帮助文件包含这个有点破碎的英文小贴士:
第一个选项符号是分隔符
编辑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 $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