我希望编写一个Windows Batch脚本,首先测试是否有任何命令行参数/?.如果是,则显示帮助消息并终止,否则执行其余的脚本代码.我尝试过以下方法:
@echo off
FOR %%A IN (%*) DO (
IF "%%A" == "/?" (
ECHO This is the help message
GOTO:EOF
)
)
ECHO This is the rest of the script
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.如果我将脚本更改为:
@echo off
FOR %%A IN (%*) DO (
ECHO %%A
)
ECHO This is the rest of the script
Run Code Online (Sandbox Code Playgroud)
并在testif.bat arg1 /? arg2我得到以下输出时调用它:
arg1
arg2
This is the rest of the script
Run Code Online (Sandbox Code Playgroud)
该FOR环似乎被忽略的/?参数.任何人都可以建议一个解决这个问题的方法吗?
Sim*_*ull 11
像这样的东西应该做的伎俩:
@echo off
IF [%1]==[/?] GOTO :help
echo %* |find "/?" > nul
IF errorlevel 1 GOTO :main
:help
ECHO You need help my friend
GOTO :end
:main
ECHO Lets do some work
:end
Run Code Online (Sandbox Code Playgroud)
感谢@jeb指出错误,如果只是/?arg提供