Batch file 如果任何命令行参数等于/,则显示帮助消息?
我希望编写一个Windows批处理脚本,首先测试任何命令行参数是否等于Batch file 如果任何命令行参数等于/,则显示帮助消息?,batch-file,cmd,Batch File,Cmd,我希望编写一个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 o
/?
。如果是这样,它将显示帮助消息并终止,否则将执行脚本代码的其余部分。我尝试了以下方法:
@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
:Loop
IF "%1"=="" GOTO Continue
IF "%1" == "/?" (
ECHO This is the help message
GOTO:EOF
)
SHIFT
GOTO Loop
:Continue
ECHO This is the rest of the script
:EOF
像这样的事情应该可以做到:
@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提供的问题是,除非参数列表包含/?
,否则我需要维护所有参数,以便脚本的其余部分可以使用它们。上述方法不能做到这一点。如果我以myBatch.bat/?
开始,则echo%*
将扩展到echo/?
,这将把echo命令的完整帮助回送到管道上,您可以放置echo参数:%*
而不是echo%*
,以避免/?
提到的@jeb问题。。。