gpt4 book ai didi

NSIS - 读取多个命令行参数

转载 作者:行者123 更新时间:2023-12-04 02:15:54 24 4
gpt4 key购买 nike

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

MyApplication.exe CLIENT="Your Mom"

我可以使用 GetParametersGetOptions 正确阅读它,如下所示:

${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)

如果您不能忍受/开关前缀,那么您将不得不自己重写 ${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

编辑:

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

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 $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

关于NSIS - 读取多个命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33647197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com