0

There is a batch script I need to write, let's call it script.bat. And I want it to have options, let's call them option1 and option2. Just like the famous --help we see everywhere.

And I would like the 4 following lines to be 4 possible commands to launch the script, according to the user's need :

script.bat -option1 value1 -option2 value2
script.bat -option2 value2 -option1 value1
script.bat -option1 value1 
script.bat -option2 value2

Now on to the script coding : I would like a neat way to handle option reading. I know about parameters readings (%0 for the program name, %1 for parameter 1 etc). But that's not very flexible since the options, as the name implies, can be optional and in any order (cf above command at line 2 of the code block).

How would you guys go about that ?

Thanks !

Aaron Blenkush
  • 3,034
  • 2
  • 28
  • 54
Myna
  • 569
  • 2
  • 10
  • 24
  • possible duplicate of [Windows Bat file optional argument parsing](http://stackoverflow.com/questions/3973824/windows-bat-file-optional-argument-parsing) – dbenham Mar 15 '13 at 03:24

2 Answers2

3

Not 100% what you were after, but how about:

@echo off

setlocal

set OPTION1=
set OPTION2=
set OPTION3=

:ARGS_LOOP
if {%1} == {} goto END_OF_ARGS
set ARG=%1

if /i "%ARG:~0,9%" == "/OPTION1:" set OPTION1=%ARG:~9%
if /i "%ARG:~0,9%" == "/OPTION2:" set OPTION2=%ARG:~9%
if /i "%ARG:~0,9%" == "/OPTION3:" set OPTION3=%ARG:~9%

shift

goto ARGS_LOOP

:END_OF_ARGS

echo Option 1 was set to [%OPTION1%]
echo Option 2 was set to [%OPTION2%]
echo Option 3 was set to [%OPTION3%]

endlocal

This can be saved as, for example, TEST.CMD and then run like:

TEST.CMD /option1:xxx /option2:yyy

Etc. The order of the options specified in the command line doesn't matter.

Simon Catlin
  • 2,141
  • 1
  • 13
  • 15
  • Sweet, thanks Simon. Just a question, could you tell me what this command does ? : if /i "%ARG:~0,9%" == "/OPTION1:" set OPTION1=%ARG:~9% – Myna Mar 15 '13 at 13:34
  • 1
    It compares the first nine characters of `ARG` to `/OPTION1:` and then sets the value of `OPTION1` to everything after the ninth character. – aphoria Mar 15 '13 at 15:13
2
@ECHO OFF
SETLOCAL
SET options=option1 option2 anotheroption
SET switches=switch1 switch2 anotherswitch
CALL :readoptions %*
FOR %%i IN (%options% %switches% badoptions) DO IF DEFINED %%i (SET %%i) ELSE (ECHO %%i NOT set)
GOTO :eof

:readoptions
FOR %%i IN (%options% %switches% badoptions) DO (SET %%i=)
:optlp
SET _parm1=%1
IF NOT DEFINED _parm1 GOTO :EOF
FOR %%i IN (%switches%) DO IF %_parm1%==-%%i SET %%i=Y&(SET _parm1=)
IF NOT DEFINED _parm1 shift&GOTO :optlp
FOR %%i IN (%options%) DO IF %_parm1%==-%%i (
SET %%i=%2
IF DEFINED %%i shift&shift&(SET _parm1=)
)
IF DEFINED _parm1 SET badoptions=%badoptions% %1&SHIFT
GOTO :optlp

If you want /options or /switches, the changes should be obvious

For any set of switches, put the switchnames in er, switches and similarly option names in options. If -switchname is found in the command line, then the variable switchname is set to Y, otherwise it is not set. Similarly, if -optionname is found then the parameter following is set as the value of the variable optionname, otherwise optionname is not set.

Any parameter which is neither a designated switchname nor optionname nor optionvalue will be gathered into badoptions

So - with the set of options and switches as shown, the command

thisbatch -switch2 -option2 optionvalue -invalid

would clear variables option1, anotheroption, switch1 and anotherswitch; set switch2=Y; set option2=optionvalue and badoptions=-invalid

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks a lot for your answer. Could you explain what this refers to : ".%_parm1%==.-%%i" I am confused by the use of the dots. – Myna Mar 15 '13 at 14:36
  • 1
    Hmm - seems to be a bit of a hangover from some earlier code. Logically they don't need to be there. The idea was to ensure that under no circumstances could the string either on the left or the right of the `==` be empty, since an empty string in these positions would generate a syntax error. Since %%i cannot be empty and each is directly preceded by an `IF NOT DEFINED` operating on the variable `_parm1` then the dots are superfluous. I'll edit them out and add some notes... – Magoo Mar 15 '13 at 14:50