batch-file - 如果任何命令行参数等于/,则显示帮助消息?

标签 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 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/

相关文章:

c - 如何使用 C 在 Windows 中执行批处理 (.bat) 文件

java - 从 Java GUI 应用程序启动 Windows 批处理文件

windows - Ctrl+C 不能可靠地终止 Windows 批处理脚本

tomcat - 无法获取 Tomcat 主页

windows - Windows 7 CMD 中的 UTF-8

windows - 如何使用批处理脚本对目录中的每个文件执行某些操作

windows - 尝试使用图形魔法绘制文本或图像时出现错误 "non-conforming drawing primitive"

html - 从网页启动 Catia 并显示谁拥有许可证

browser - 可以通过浏览器访问但无法ping通?

java - 为什么从 cmd 调用 jar 时 java 扫描仪不起作用?