解析Windows批处理文件参数

时间:2013-07-09 08:03:15

标签: batch-file

我有这个用于识别每组批处理文件顶部的帮助参数。是否有更简洁的方式,或者这样做是否合适?

@Echo Off
if "%1" == "" Goto Usage
if "%1" == "-?" Goto Usage
if "%1" == "/?" Goto Usage
if "%1" == "/help" Goto Usage
if "%1" == "-help" Goto Usage
if "%1" == "/Help" Goto Usage

2 个答案:

答案 0 :(得分:2)

你不能缩短它:

if "%~1" == "" Goto Usage
if "%~1" == "-?" Goto Usage
if "%~1" == "/?" Goto Usage
if /i "%~1" == "/help" Goto Usage
if "%~1" == "-help" Goto Usage

if /i表示忽略大小写。 "%~1"以避免双重报价
您可以在最后询问所有有用的答案和goto usage

if /i "%~1" == "a" Goto :doit_a
if /i "%~1" == "b" Goto :doit_b
if /i "%~1" == "c" Goto :doit_c
goto :Usage

答案 1 :(得分:2)

setlocal enableDelayedExpansion
set "help= -? /? -help /help "
if "%~1" equ "" goto Usage
if "!help: %~1 =!" neq "%help%" goto Usage