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
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?
Best Answer
I have managed to translate the majority of this script to work in a Windows 7 batch file. I have not tested this against any other version of Windows.
This program scans the command line sent to it and sets various environment variables that coorespond to the settings.
It sets an OPTION_arg variable for each arg on the command line. If a switch, the env var is set to 1. If a value is given via the colon sign, it's set to that value. Note, there can not be any white space around the
:
[Modification] This modified script also cannot handle option or parameter values containing spaces, even when encased in parenthesis. [/Modification]Use
If defined OPTION_arg
orif /i "%OPTION_arg%"=="value"
to test for optionsIt also sets a parameter variable for each paramater entered:
PARAM_1
toPARAM_n
andPARAM_0
is a special value that contains the number of PARAMs. Useful for looping through all of them. For examplefor /l (1,1,%PARAM_0%) do ...
In your batch file call getopt as
call GetOpt.bat %*
I also recommend setting
setlocal
andendlocal
in the host batch file so that the option and param variables do not stick around after the host batch files exits.The parts I have not translated are:
<SPACE>
Here is the translated script.