简体   繁体   English

NSIS-读取多个命令行参数

[英]NSIS - Reading multiple command line arguments

When passing a single argument to my windows installer, 将单个参数传递给Windows安装程序时,

MyApplication.exe CLIENT="Your Mom"

I can correctly read it using GetParameters and GetOptions , like so: 我可以使用GetParametersGetOptions正确读取它,如下所示:

${GetParameters} $0
${GetOptions} "$0" "CLIENT=" $CLIENT 

But as soon as I attempt to pass multiple parameters 但是,一旦我尝试传递多个参数

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

and try to read it like so 并尝试像这样阅读

${GetParameters} $0
${GetOptions} "$0" "CLIENT=" $CLIENT 
${GetOptions} "$0" "NAME=" $NAME 
${GetOptions} "$0" "LOCATION=" $LOCATION 
${GetOptions} "$0" "TEL=" $TEL

The values of $CLIENT , $NAME and $TEL would be correct, but $LOCATION will contain $CLIENT$NAME$TEL值正确,但是$LOCATION将包含

Da Yard TEL=0221456789

The same happens if I add more parameters, the first two and last one is always correct, but the parameters in between always contains some substring of the string passed to the installer. 如果我添加更多参数,也会发生同样的情况,前两个参数和最后一个参数始终正确,但是之间的参数始终包含传递给安装程序的字符串的一些子字符串。

Am I using GetOptions correctly? 我是否正确使用GetOptions

If you change all the parameters so they start with / then it works for some reason! 如果更改所有参数,使其以/开头,则出于某些原因它会起作用! ( MyApplication.exe /CLIENT="Your Mom" /NAME="Jill" /LOCATION="Da Yard" /TEL="0221456789" and ${GetOptions} "$0" "/LOCATION=" $LOCATION ) MyApplication.exe / CLIENT =“您的妈妈” / NAME =“ Jill” / LOCATION =“ Da Yard” / TEL =“ 0221456789”${GetOptions} "$0" "/LOCATION=" $LOCATION

If you cannot live with the / switch prefix then you would have to rewrite ${GetOptions} yourself or wait until the next NSIS release where it will hopefully be fixed. 如果不能使用/开关前缀,则必须自己重写$ {GetOptions}或等到下一个NSIS版本(希望该版本可以修复)。

If you want to take a stab at fixing it yourself then it would look something like this: 如果您想自己动手修复它,那么它将看起来像这样:

!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

Edit: 编辑:

It seems like this might be by design, the help file contains this little broken English tidbit: 看来这可能是设计使然,帮助文件包含这个破损的英文提示:

First option symbol it is delimiter 第一个选项符号是定界符

Edit2: 编辑2:

I made a new GetOptions from scratch, it only supports double quotes and is probably different in all sorts of ways. 我从头开始制作了一个新的GetOptions,它仅支持双引号,并且可能在各种方面都不同。 I did not test much but it seems to work OK: 我没有进行太多测试,但是似乎可以正常工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM