我希望编写一个 Windows 批处理脚本,首先测试是否有任何命令行参数等于 /?
。如果是,则显示帮助消息并终止,否则执行脚本代码的其余部分。我尝试过以下方法:
@echo off
FOR %%A IN (%*) DO (
IF "%%A" == "/?" (
ECHO This is the help message
GOTO:EOF
)
)
ECHO This is the rest of the script
这似乎不起作用。如果我将脚本更改为:
@echo off
FOR %%A IN (%*) DO (
ECHO %%A
)
ECHO This is the rest of the script
并将其命名为 testif.bat arg1/? arg2
我得到以下输出:
arg1
arg2
This is the rest of the script
FOR
循环似乎忽略了 /?
参数。谁能建议一种有效的方法来解决这个问题?
最佳答案
像这样的事情应该可以解决问题:
@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
感谢@jeb指出错误,如果只有/?提供arg
关于batch-file - 如果任何命令行参数等于/,则显示帮助消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684259/