窗户BAT文件可选参数解析 [英] Windows Bat file optional argument parsing
问题描述
我需要我的bat文件接受多个可选的命名参数。
I need my bat file to accept multiple optional named arguments.
mycmd.bat man1 man2 -username alice -otheroption
例如我的命令有2强制性参数和两个可选参数(-username),有爱丽丝的参数值,-otheroption:
For example my command has 2 mandatory parameters, and two optional parameters (-username) that has an argument value of alice, and -otheroption:
我希望能够将这些值采摘到变量。
I'd like to be able to pluck these values into variables.
只是把到任何一个电话一个已经解决了这个。曼这些蝙蝠文件是一种痛苦。
Just putting out a call to anyone that has already solved this. Man these bat files are a pain.
推荐答案
虽然我倾向于@AlekDavis同意,是否仍有几种方法在NT外壳做到这一点。
Though I tend to agree with @AlekDavis, there are nonetheless several ways to do this in the NT shell.
我想借此 SHIFT 命令的方法和的IF 条件分支,这样的事情...
The approach I would take advantage of the SHIFT command and IF conditional branching, something like this...
@ECHO OFF
SET man1=%1
SET man2=%2
SHIFT & SHIFT
:loop
IF NOT "%1"=="" (
IF "%1"=="-username" (
SET user=%2
SHIFT
)
IF "%1"=="-otheroption" (
SET other=%2
SHIFT
)
SHIFT
GOTO :loop
)
ECHO Man1 = %man1%
ECHO Man2 = %man2%
ECHO Username = %user%
ECHO Other option = %other%
REM ...do stuff here...
:theend
这篇关于窗户BAT文件可选参数解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!