在选择特定选项时包括另一个 option/file
Include another option/file While selecting specific Options
我制作了一个简单的批处理文件,通过启动时提供的一些选项来执行可执行文件。
有些事情是这样的:
:A
Echo Option 1
Echo Option 2
Set /p set1=Choice :
if %set1%==1 set A=Set1_1
if %set1%==2 set A=Set1_2
goto Set_2
:B
Echo Option A
Echo Option B
Set /p set2=Choice :
if %set2%==A set B=Set2_A
if %set2%==B set B=Set2_B
goto launch
:launch
program.exe -%A% -%B%
所以基本上这是可行的。但是我需要的是一种方法,如果同时选择了 "Option 1" 和 "Option A",则可以为我的程序包含另一个启动参数。不在 "Option 2" 和 "Option B".
中
所以我的发布看起来像这样
program.exe -%set1% -%set2% -%if1_A%
编辑:我在此命令行中犯了一些错误,但自从@avery_larry 指出以来我不会更正它。
很抱歉让我感到困惑,如果需要进一步澄清或详细说明,请告诉我。 :)
未测试
如果一个变量没有被设置为任何东西,那么它将展开为空。设置您的第三个变量,并确保在不满足条件时不设置它。像这样:
set if1_a=
if "%set1%"=="1" (
if "%set2%"=="A" (
set "if1_a=-option"
)
)
program.exe -%set1% -%set2% %if1_A%
请注意,我将连字符作为变量的一部分。它也可能应该是:
program.exe -%A% -%B% %if1_A%
最后您可能想使用 if /i
使其不区分大小写。
可以通过使用输入验证函数来避免用户输入问题,如下所示:
@Echo off & CD "%~dp0"
TITLE %~n0 & Setlocal
::: / Creates variable /AE = Ascii-27 escape code.
::: - http://www.dostips.com/forum/viewtopic.php?t=1733
::: -
:::
::: - /AE can be used with and without DelayedExpansion.
Setlocal
For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
Endlocal
Set "/AE=%%a"
)
::: \
%= Remark =%
(Set LF=^
%= NewLine variable to split set /p input line =%)
%= create blank batch file to store and retrieve variables in for automatic reload =%
If not Exist "inputlog.bat" (Echo(@Echo Off>inputlog.bat & Goto :main)
For %%A in ( "%/AE%[36m[%/AE%[34mN%/AE%[36m]ew" , "[%/AE%[34mL%/AE%[36m]oad%/AE%[0m" )Do Echo.%%~A
Choice /N /C nl >nul
If errorlevel 2 (Goto :load)
Goto :Main
:Input <VarName> <Optional - define valid values with each additional parameter>
Set "variable=%~1"
Setlocal EnableDelayedExpansion
IF not "%~2"=="" For %%A in (%*) Do (If not "%%~A"=="%1" (Set "valid=%%~A !valid!"))
:Confirm
%= allow input of variable, test input, store for reloading. =%
Echo(%/AE%[35m
Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
IF "!input!"=="" (
Echo(%/AE%[31m!variable! required.
Goto :Confirm
)
IF "%~2"=="" (
ECHO(Set "!variable!=!input!">>inputlog.bat
Endlocal & Set "%variable%=%input%"
Exit /B
)
For %%A in (!valid!) Do (
If /I "!input!"=="%%~A" (
ECHO(Set "!variable!=!input!">>inputlog.bat
Endlocal & Set "%variable%=%input%"
Exit /B
)
)
Echo(%/AE%[31m!variable! required.
Echo(%/AE%[36mSelect from:
For %%A in (!valid!) Do (Echo(%/AE%[33m%%~A%/AE%[0m)
Goto :Confirm
:main
%= define variable (parameter 1) and restrict definition of variables to specified values (additional parameters) =%
Call :Input language english spanish
%= Define multiple variables at once =%
For %%A in ("name" "age" "location" "job" "hobbies") Do (Call :Input %%A)
%= undefine local variables =%
endlocal
%= Display that variable definitions have been removed =%
For %%A in ("language" "name" "age" "location" "job" "hobbies") Do Set %%A
:load
%= Reload the variables from the input Log and display. =%
Call inputlog.bat
%= Demonstrate that variables have been reloaded =%
Setlocal EnableDelayedExpansion
For %%A in ("language" "name" "age" "location" "job" "hobbies") Do (Echo(%/AE%[37m%%~A:!LF!%/AE%[36m%/AE%[33m!%%~A!%/AE%[32m%/AE%[0m)
pause
虽然您的问题可能只是为了问题的目的而使用输入选项,而不是现实世界中的输入选项;因为它们是已知选项,所以我决定提供一个使用更强大的内置 choice.exe
命令的解决方案:
@Rem Undefine any existing variables to be used.
@For /F "Delims==" %%G In ('Set Opt[ 2^>NUL')Do @Set "%%G="
@Rem Request first argument.
@"%__APPDIR__%choice.exe" /C 12 /M "First argument"
@Rem Define the first argument with the value chosen.
@Set "Opt[1]=%ERRORLEVEL%"
@Rem Request second argument.
@"%__APPDIR__%choice.exe" /C AB /M "Second argument"
@Rem Define the second argument with the value chosen.
@If ErrorLevel 2 (Set "Opt[2]=B")Else (Set "Opt[2]=A"
Rem Define third argument if first chosen was 1
If %Opt[1]% Equ 1 Set "Opt[3]=-"Third option string"")
@Rem Launch program with required options.
@Echo program.exe -"%Opt[1]%" -"%Opt[2]%" %Opt[3]%
@Rem Prevent cmd window from possibly closing early.
@Pause
我制作了一个简单的批处理文件,通过启动时提供的一些选项来执行可执行文件。
有些事情是这样的:
:A
Echo Option 1
Echo Option 2
Set /p set1=Choice :
if %set1%==1 set A=Set1_1
if %set1%==2 set A=Set1_2
goto Set_2
:B
Echo Option A
Echo Option B
Set /p set2=Choice :
if %set2%==A set B=Set2_A
if %set2%==B set B=Set2_B
goto launch
:launch
program.exe -%A% -%B%
所以基本上这是可行的。但是我需要的是一种方法,如果同时选择了 "Option 1" 和 "Option A",则可以为我的程序包含另一个启动参数。不在 "Option 2" 和 "Option B".
中所以我的发布看起来像这样
program.exe -%set1% -%set2% -%if1_A%
编辑:我在此命令行中犯了一些错误,但自从@avery_larry 指出以来我不会更正它。
很抱歉让我感到困惑,如果需要进一步澄清或详细说明,请告诉我。 :)
未测试
如果一个变量没有被设置为任何东西,那么它将展开为空。设置您的第三个变量,并确保在不满足条件时不设置它。像这样:
set if1_a=
if "%set1%"=="1" (
if "%set2%"=="A" (
set "if1_a=-option"
)
)
program.exe -%set1% -%set2% %if1_A%
请注意,我将连字符作为变量的一部分。它也可能应该是:
program.exe -%A% -%B% %if1_A%
最后您可能想使用 if /i
使其不区分大小写。
可以通过使用输入验证函数来避免用户输入问题,如下所示:
@Echo off & CD "%~dp0"
TITLE %~n0 & Setlocal
::: / Creates variable /AE = Ascii-27 escape code.
::: - http://www.dostips.com/forum/viewtopic.php?t=1733
::: -
:::
::: - /AE can be used with and without DelayedExpansion.
Setlocal
For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
Endlocal
Set "/AE=%%a"
)
::: \
%= Remark =%
(Set LF=^
%= NewLine variable to split set /p input line =%)
%= create blank batch file to store and retrieve variables in for automatic reload =%
If not Exist "inputlog.bat" (Echo(@Echo Off>inputlog.bat & Goto :main)
For %%A in ( "%/AE%[36m[%/AE%[34mN%/AE%[36m]ew" , "[%/AE%[34mL%/AE%[36m]oad%/AE%[0m" )Do Echo.%%~A
Choice /N /C nl >nul
If errorlevel 2 (Goto :load)
Goto :Main
:Input <VarName> <Optional - define valid values with each additional parameter>
Set "variable=%~1"
Setlocal EnableDelayedExpansion
IF not "%~2"=="" For %%A in (%*) Do (If not "%%~A"=="%1" (Set "valid=%%~A !valid!"))
:Confirm
%= allow input of variable, test input, store for reloading. =%
Echo(%/AE%[35m
Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
IF "!input!"=="" (
Echo(%/AE%[31m!variable! required.
Goto :Confirm
)
IF "%~2"=="" (
ECHO(Set "!variable!=!input!">>inputlog.bat
Endlocal & Set "%variable%=%input%"
Exit /B
)
For %%A in (!valid!) Do (
If /I "!input!"=="%%~A" (
ECHO(Set "!variable!=!input!">>inputlog.bat
Endlocal & Set "%variable%=%input%"
Exit /B
)
)
Echo(%/AE%[31m!variable! required.
Echo(%/AE%[36mSelect from:
For %%A in (!valid!) Do (Echo(%/AE%[33m%%~A%/AE%[0m)
Goto :Confirm
:main
%= define variable (parameter 1) and restrict definition of variables to specified values (additional parameters) =%
Call :Input language english spanish
%= Define multiple variables at once =%
For %%A in ("name" "age" "location" "job" "hobbies") Do (Call :Input %%A)
%= undefine local variables =%
endlocal
%= Display that variable definitions have been removed =%
For %%A in ("language" "name" "age" "location" "job" "hobbies") Do Set %%A
:load
%= Reload the variables from the input Log and display. =%
Call inputlog.bat
%= Demonstrate that variables have been reloaded =%
Setlocal EnableDelayedExpansion
For %%A in ("language" "name" "age" "location" "job" "hobbies") Do (Echo(%/AE%[37m%%~A:!LF!%/AE%[36m%/AE%[33m!%%~A!%/AE%[32m%/AE%[0m)
pause
虽然您的问题可能只是为了问题的目的而使用输入选项,而不是现实世界中的输入选项;因为它们是已知选项,所以我决定提供一个使用更强大的内置 choice.exe
命令的解决方案:
@Rem Undefine any existing variables to be used.
@For /F "Delims==" %%G In ('Set Opt[ 2^>NUL')Do @Set "%%G="
@Rem Request first argument.
@"%__APPDIR__%choice.exe" /C 12 /M "First argument"
@Rem Define the first argument with the value chosen.
@Set "Opt[1]=%ERRORLEVEL%"
@Rem Request second argument.
@"%__APPDIR__%choice.exe" /C AB /M "Second argument"
@Rem Define the second argument with the value chosen.
@If ErrorLevel 2 (Set "Opt[2]=B")Else (Set "Opt[2]=A"
Rem Define third argument if first chosen was 1
If %Opt[1]% Equ 1 Set "Opt[3]=-"Third option string"")
@Rem Launch program with required options.
@Echo program.exe -"%Opt[1]%" -"%Opt[2]%" %Opt[3]%
@Rem Prevent cmd window from possibly closing early.
@Pause