Gha*_*ing 8 windows batch command-line
I have found a very useful script here that will parse in arguments to a batch file and process them as follows:
BatchFile.btm /a /b:22 /longopt Parm1 Parm2 /quotedArg:"long quoted arg"
- OPTION_a will equal 1.
- OPTION_b will equal 22
- OPTION_quotedArg will equal "long quoted arg"
- OPTION_longopt will eqal 1.
- PARAM_1 will equal Parm1
- PARAM_2 will equal Parm2
- PARAM_0 will be set to the number of parms, so 2 in this case
Run Code Online (Sandbox Code Playgroud)
However, this script is written for .btm files and doesn't seem to be suitable for the DOS emulator or more recent versions of Windows. Can anyone translate this or know where to find an up-to-date equivalent that will work in DOS emulator in Win7/Svr2003?
我已经设法将这个脚本的大部分翻译成在 Windows 7 批处理文件中工作。我尚未针对任何其他版本的 Windows 对此进行测试。
该程序扫描发送给它的命令行,并设置与这些设置相呼应的各种环境变量。
它为命令行上的每个 arg 设置一个 OPTION_arg 变量。如果是开关,则 env var 设置为 1。如果通过冒号符号给出值,则将其设置为该值。请注意,:
[Modification]周围不能有任何空格。此修改后的脚本也无法处理包含空格的选项或参数值,即使括在括号中也是如此。[/修改]
使用If defined OPTION_arg或if /i "%OPTION_arg%"=="value"测试选项
它还为输入的每个参数设置一个参数变量:PARAM_1toPARAM_n和
PARAM_0是一个包含 PARAM 数量的特殊值。用于遍历所有这些。例如for /l (1,1,%PARAM_0%) do ...
在您的批处理文件中调用 getopt 作为 call GetOpt.bat %*
我还建议在主机批处理文件中设置setlocal和endlocal,以便在主机批处理文件退出后选项和参数变量不会保留。
Example usage: HostBatchFile.bat /a /b:22 /longopt Parm1 Parm2
OPTION_a will equal 1.
OPTION_b will equal 22
OPTION_longopt will eqal 1.
PARAM_1 will equal Parm1
PARAM_2 will equal Parm2
PARAM_0 will be set to the number of parms, so 2 in this case
Run Code Online (Sandbox Code Playgroud)
我没有翻译的部分是:
<SPACE>这是翻译后的脚本。
@echo off
cls
set getopt_ParmCounter=1
set paramc=1
set DEBUG=1
set argc=0
for %%x in (%*) do Set /A argc+=1
echo Number of arguments: %argc%
echo %*&echo.
set _myvar=%*
rem Loop through all command line arguments one at a time
:varloop
set isparam=1
for /f "tokens=1*" %%a in ('echo %_myvar%') DO (
set getopt_Parm=%%a
set _myvar=%%b
call :paramtype
rem shift along arguments and rerun loop
if NOT "%%b"=="" goto varloop
)
goto :eof
:paramtype
rem If first character starts with a - or / it must be an option
if /i "%getopt_Parm:~0,1%"=="-" call :option
if /i "%getopt_Parm:~0,1%"=="/" call :option
if /i "%isparam%"=="1" call :param
goto :eof
:option
set isparam=0
rem Set the Equal Index to the position of the colon. 0 means none was found
for /f %%j in ('findstring %getopt_Parm% :') do set getopt_EqIdx=%%j
rem If the index is GE 0 then we must have a colon in the option.
if /i "%getopt_EqIdx%"=="0" (call :nocolon) else (call :colon)
goto :eof
:colon
rem set the OPTION value to the stuff to the right of the colon
set /a getopt_ParmNameEnd=%getopt_EqIdx%-2
call set getopt_ParmName=%%getopt_Parm:~1,%getopt_ParmNameEnd%%%
call set getopt_ParmValue=%%getopt_Parm:~%getopt_EqIdx%%%
set OPTION_%getopt_ParmName%=%getopt_ParmValue%
goto :eof
:nocolon
rem This is a flag, so simply set the value to 1
set getopt_ParmName=%getopt_Parm:~1%
set getopt_ParmValue=1
set OPTION_%getopt_ParmName%=%getopt_ParmValue%
goto :eof
:param
rem There was no / or - found, therefore this must be a paramater, not an option
set PARAM_%getopt_ParmCounter%=%getopt_Parm%
set PARAM_0=%getopt_ParmCounter%
set /a getopt_ParmCounter+=1
goto :eof
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12960 次 |
| 最近记录: |