4

我希望编写一个 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循环似乎忽略了参数/?。任何人都可以提出解决这个问题的方法吗?

4

2 回答 2

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

感谢@jeb 指出错误,如果只有 /? 提供的 arg

于 2012-12-03T13:49:48.500 回答
0

不要使用 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
于 2012-12-03T13:44:22.310 回答