Migrated From JP Software Wiki
Overview
There are often times one wishes to process command line options and parameters in a consistent way, and not duplicate parsing in every bath file. GetOpt is a common and freely available option parser and this batch file attempts to mimic a few of it's basic functions within the limitations of TakeCommand.
There are two main categories of command line items.
I hope that the following example may help illustrate.
Examples:
So, as an example, to process all items listed on the command line you could:
Or, to perform an action based on a boolean switch you could:
I would appreciate any comments or suggestions I could use to improve this program.
Michael Fross
michael _at_ fross _dot_ org
GetOpt.btm
Overview
There are often times one wishes to process command line options and parameters in a consistent way, and not duplicate parsing in every bath file. GetOpt is a common and freely available option parser and this batch file attempts to mimic a few of it's basic functions within the limitations of TakeCommand.
There are two main categories of command line items.
- Program Parameters. These are inputs for the progam and are entered in on the command line without switch characters. For example, the command "copy a b" has two parameters, a and b.
- Program Switches. These typically change the way the command works. They are usually prefaced with a dash (-) or a slash (/). For example, the command "copy /q a b" has one boolean switch. Switches may also have values associated with them, so the command "copy /q /s:1 a b" is valid.
- If you wish to see a lot of debug information on what's going on, execute "set debug=1" before running.
- Call getopt.btm in your batch file by executing: call GetOpt.btm %$
- OPTION_x will be set for each option where x is the option name. If the option equals a value, it will be set that value, if not it will be set to 1.
- PARAM_x will be a sequential number each equal to the parameter value.
- PARAM_0 is a special case and will equal the number of parameters entered
- It is highly recommended that your batch file by enclosed by "SetLocal" and "EndLocal" commands. This will prevent the variables set in GetOpt from living after your program has terminated. You could also add:
unset getopt* OPTION_* PARAM_*
at the end of your program to clean up the variables that are set.
I hope that the following example may help illustrate.
Examples:
Code:
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
So, as an example, to process all items listed on the command line you could:
Code:
call GetOpt.btm %$
do i=1 to %PARM_0 by 1
echo Processing %PARM_$i
gosub ProcessParm
enddo
Or, to perform an action based on a boolean switch you could:
Code:
call GetOpt.btm %$
iff defined %OPTION_v then
gosub DisplayVerion
endiff
Michael Fross
michael _at_ fross _dot_ org
GetOpt.btm
Code:
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: GetOpt - Process command line options
::
:: Michael Fross
:: [email][email protected][/email]
:: [url]http://fross.org[/url]
::
:: 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 ':'
::
:: Use "If defined OPTION_arg" or "If %OPTION_arg eq value" to test for options
::
:: It also sets a parameter variable for each paramater entered: PARAM_1 to PARAM_n
:: PARAM_0 is a special value that contains the number of PARAMs. Useful for looping
:: through all of them. For example, do i = 1 to %PARAM_0 by 1 ...
::
:: In your batch file call getopt as:
:: call GetOpt.btm %$
::
:: I also recommend setting setlocal and endlocal in the host batch file so that
:: the option / param variable do not stick around after the host batch files exits.
::
:: Example usage: 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
::
:: To get debug messages, set DEBUG=1. This will give detailed information for each
:: parameter on the command line as getopt loops through the list.
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Clean up the environment before we get going
unset getopt* OPTION_* PARAM_*
set getopt_ParmCounter=1
:: If in debug mode, kick off the display by showing the number of arguments
if defined DEBUG echo GetOpt is processing %# arguments:
:: Loop through all command line arguments one at a time.
for /L %i in (1,1,%#) do (
if defined DEBUG (echo. %+ echo Scan #%i:)
:: If first character starts with a - or / it must be an option
iff %@instr[0,1,%[%i]] == - .or. %@instr[0,1,%[%i]] == / then
set getopt_Parm=%[%i]
if defined DEBUG echo - Item "%getopt_Parm" is an option.
:: Set the Equal Index to the position of the colon. 0 means none was found
set getopt_EqIdx=%@index[%getopt_Parm,:]
:: Display the index position of the colon
if defined DEBUG .AND. %getopt_EqIdx GE 0 echo - Found colon at index position "%getopt_EqIdx"
:: If the index is GE 0 then we must have a colon in the option.
:: set the OPTION value to the stuff to the right of the colon
iff %getopt_EqIdx ge 0 then
set getopt_ParmName=%@instr[2, %@Dec[%getopt_EqIdx] , %getopt_Parm]
if defined DEBUG echo - ParmName = "%getopt_ParmName"
set getopt_ParmValue=%@right[%@eval[-%getopt_EqIdx-1],%getopt_Parm]
if defined DEBUG echo - Parmvalue = "%getopt_ParmValue"
set OPTION_%getopt_ParmName=%getopt_ParmValue
else
:: This is a flag, so simply set the value to 1
if defined DEBUG echo - No colon found in "%getopt_Parm"
set getopt_ParmName=%@right[%@Dec[%@len[%getopt_Parm]],%getopt_Parm]
set getopt_ParmValue=1
if defined DEBUG echo - ParmName = "%getopt_ParmName"
set OPTION_%getopt_ParmName=%getopt_ParmValue
endiff
:: Regardless if there was a value or not, display what is going to occur
if defined DEBUG echo - Setting Variable OPTION_%getopt_ParmName=%getopt_ParmValue
else
:: There was no / or - found, therefore this must be a paramater, not an option
if defined DEBUG echo - "%[%i]" is a parameter, not an option
set PARAM_%getopt_ParmCounter=%[%i]
set PARAM_0=%getopt_ParmCounter
if defined DEBUG echo - Updating Number of Parms. PARAM_0=%PARAM_0
if defined DEBUG echo - Setting Variable PARAM_%getopt_ParmCounter = %[%i]
set getopt_ParmCounter=%@Inc[%getopt_ParmCounter]
endiff
)
:: Display additional information
iff defined DEBUG then
echo.
echo There were %PARAM_0 parameters found. Setting PARAM_0=%PARAM_0
echo.
echo GetOpt has completed processing %# arguments. Ending Execution.
endiff
:: Perform cleanup
unset getopt_*