Home > database >  How to get optional batch argument in format "-argument" "value"?
How to get optional batch argument in format "-argument" "value"?

Time:07-29

Can I write a batch file with optional input argument in form of "-argument" "value" like a python command?

Something like this:

script.bat -argument1 value1 -argument2 value2 -argument3 value3

In which I can analyze and get value of each argument to pass onto another script?

Thank you.

CodePudding user response:

Quite easy: get the arguments in pairs, shift and repeat until there are no more arguments:

@echo off
setlocal
:loop
if "%~1" == "" goto :noMoreParams
set "switch=%~1"
set "value=%~2"
set "%switch%=%value%"
shift & shift
goto :loop
:NoMoreParams
echo Done. Your params are:
set -

Optional, you can check for proper syntax, like if not "%switch:~0,1%" == "-" goto :wrongSyntax

  • Related