@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: batchparms.bat = This is an example of named parameters in a batch file. ::
:: This demonstrates the ability to name parameters instead of the current ::
:: positional restrictions provided natively. ::
:: ::
:: Parameters: ::
:: -first = First named parm. Should be AAA BBB or CCC [Default is AAA] ::
:: -second = Second named parm. Should be XXX YYY or ZZZ [Default is ZZZ] ::
:: -flagone = First named switch [Default is TRUE] ::
:: -flagtwo = Second named switch [Default is TRUE] ::
:: ::
:: 02/19/2010 - HighDesertRaider ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::
:: 02/22/2012 - Modified to be used as a Demo ::
:: - HighDesertRaider ::
:: ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Set Environment
::
:: These flags need to be set off to use named parameters
::
SET FLAG_FIRST=0
SET FLAG_SECOND=0
SET FLAG_ONE=0
SET FLAG_TWO=0
::
:: Set Default Values
::
CLS
SET PARM_FIRST=AAA
SET PARM_SECOND=ZZZ
SET PARM_ONE=1
SET PARM_TWO=1
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Parse the input parameters, and assign the arguments to variables. ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FOR %%A in (%*) DO (
IF !FLAG_FIRST!==1 (
SET PARM_FIRST=%%A
SET FLAG_FIRST=0
) ELSE (
IF !FLAG_SECOND!==1 (
SET PARM_SECOND=%%A
SET FLAG_SECOND=0
) ELSE (
IF /I %%A==-FIRST (
SET FLAG_FIRST=1
) ELSE (
IF /I %%A==-SECOND (
SET FLAG_SECOND=1
) ELSE (
IF /I %%A==-FLAGONE (
SET PARM_ONE=0
) ELSE (
IF /I %%A==-FLAGTWO (
SET PARM_TWO=0
) ELSE (
SET BADPARMMSG=Parameter %%A is not a valid option.
GOTO:BadFormExit)))))))
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Validate -ParmFirst ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ValidateParmFirst
ECHO Validate -ParmFirst %PARM_FIRST%
FOR %%A in (AAA BBB CCC) DO (
IF /I %PARM_FIRST%==%%A (
GOTO ValidParmFirst))
::
:: A Valid Parm Value was not found in the list
::
SET BADPARMMSG=Value %PARM_FIRST% is not a valid value for -ParmFirst
ECHO Please select one of the following values:
ECHO AAA
ECHO BBB
ECHO CCC
GOTO BadFormExit
:ValidParmFirst
ECHO Value %PARM_FIRST% is valid for -ParmFirst
ECHO.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Validate -ParmSecond ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ValidateParmSecond
ECHO Validate -ParmSecond %PARM_SECOND%
FOR %%A in (XXX YYY ZZZ) DO (
IF /I %PARM_SECOND%==%%A (
GOTO ValidParmSecond))
::
:: A Valid Parm Value was not found in the list
::
SET BADPARMMSG=Value %PARM_FIRST% is not a valid value for -ParmSecond
ECHO Please select one of the following values:
ECHO XXX
ECHO YYY
ECHO ZZZ
GOTO BadFormExit
:ValidParmSecond
ECHO Value %PARM_SECOND% is valid for -ParmSecond
ECHO.
::
::
::
ECHO.
ECHO The value of -first is %PARM_FIRST%
ECHO The value of -second is %PARM_SECOND%
ECHO The value of -flagone is %PARM_ONE%
ECHO The value of -flagtwo is %PARM_TWO%
ECHO.
GOTO:RunMyCodeCauseIGotGoodParms
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This function shows the user the correct form to use ::
:: and exits the script. ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:BadFormExit
ECHO.
ECHO Sorry friend, but your form is bad.
ECHO.
ECHO batchparms.bat %* is not correct.
ECHO Reason: %BADPARMMSG%
ECHO.
ECHO the correct syntax for this command is:
ECHO "batchparms.bat [-first AAA | BBB | CCC] [-second XXX | YYY | ZZZ] [-flagone] [-flagtwo]"
EXIT /B 1
:RunMyCodeCauseIGotGoodParms
::
:: Insert Code Here to Run once Parms are Validated
::
EXIT /B
根据http://www.robvanderwoude.com/parameters.php你可以用 if 检查它们:
或者
或者
您可以在 %1 的值上进行分支。例如,一种方法是
所以这是我对这个问题的解决方案。我已经使用这种样式来使用传统位置值的“命名参数”。这样,可以在默认和使用的参数上进行验证。
参数值可以列在一个文件中,这样您就不必更改批处理文件,只需添加一个新值。并且可以添加更多文本来解释值等。
你可以试试这个
设置测试=%1
如果 %test%.==. (未设置回声测试)