gpt4 book ai didi

batch-file - 批处理文件 - 验证参数

转载 作者:行者123 更新时间:2023-12-04 04:37:05 25 4
gpt4 key购买 nike

我有一个批处理文件,它应该接受输入“-input1”、“-input2”和“-input3”。在这里,我需要验证参数,例如如果用户将输入参数作为“-test”或“-blah”或任何其他参数提供,那么我应该将错误作为“未重新调整的参数”抛出。我怎样才能做到这一点?我尝试将参数迭代为 %1 %2 等,但我无法实现所需的行为。

IF "%1"=="" (
GOTO :ERROR3
)
IF "%1"=="-input1" (
SET value1=%2
)
@IF "%3"=="-input1" (
SET value1=%4
)
@IF "%5"=="-input1" (
SET value1=%6
)
@IF "%7"=="-input1" (
SET value1=%8
)
IF "%1"=="-input2" (
SET value1=%2
)
@IF "%3"=="-input2" (
SET value2=%4
)
@IF "%5"=="-input2" (
SET value2=%6
)
@IF "%7"=="-input2" (
SET value2=%8
)
IF "%1"=="-input3" (
SET value3=%2
)
@IF "%3"=="-input3" (
SET value3=%4
)
@IF "%5"=="-input3" (
SET value3=%6
)
@IF "%7"=="-input3" (
SET value3=%8
)

// some codes here

:ERROR1
ECHO "ERROR1"
GOTO :END
:ERROR2
ECHO "ERROR1"
GOTO :END
:ERROR3
ECHO Usage SAMPLE.bat -input1 "value1" -input2 "value2" -input3 "value3"
:END

任何帮助..

最佳答案

这是一个简单的参数转换示例

@echo off
setlocal
set "value1="
set "value2="
set "value3="

:Parse
set "Param=%~1"
if not defined Param goto Validate
if "%Param%"=="-input1" set "value1=%2" & goto Next
if "%Param%"=="-input2" set "value2=%2" & goto Next
if "%Param%"=="-input3" set "value3=%2" & goto Next
goto Error1

:Next
shift & shift
goto Parse

:Validate
if defined value1 if defined value2 if defined value3 goto Main
goto Error2

:Main
echo %value1% %value2% %value3%
goto End

:Error1
echo Invalid Parameter: %1 %2
goto End

:Error2
echo Usage %~nx0 -input1 "value1" -input2 "value2" -input3 "value3"
goto End

:End
endlocal

关于batch-file - 批处理文件 - 验证参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19569889/

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