@ECHO OFF
SETLOCAL
SET "switches=name age whatever"
SET "noargs=option anotheroption"
SET "swindicators=/ -"
SET "otherparameters="
:: clear switches
FOR %%a IN (%switches% %noargs%) DO SET "%%a="
:swloop
IF "%~1"=="" GOTO process
FOR %%a IN (%swindicators%) DO (
FOR %%b IN (%noargs%) DO (
IF /i "%%a%%b"=="%~1" SET "%%b=Y"&shift&GOTO swloop
)
FOR %%b IN (%switches%) DO (
IF /i "%%a%%b"=="%~1" SET "%%b=%~2"&shift&shift&GOTO swloop
)
)
SET "otherparameters=%otherparameters% %1"
SHIFT
GOTO swloop
:process
FOR %%a IN (%switches% %noargs% otherparameters) DO IF DEFINED %%a (CALL ECHO %%a=%%%%a%%) ELSE (ECHO %%a NOT defined)
GOTO :EOF
Here's a demonstration.
Given you have 3 switches, name age whatever which accept one argument and two true switches, option and anotheroption which take no arguments, then running the above procedure as
q27497516 -age 92 /option goosefeathers /name fred
(q27497516.bat is my batchname here) will yield
name=fred
age=92
whatever NOT defined
option=Y
anotheroption NOT defined
otherparameters= goosefeathers
Convention is to use /
as a switch indicator - a leading -
is legitimate for a filename. Implementation depends on the programmer's whim. The above structure will allow any character - within cmd
's syntax rules, naturally [ie. $_# OK, %!^ No, ! maybe () awkward]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…