Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for Unix style 'getopt' command line parsing in a Windows batch file

Can anyone help me find something to parse command line args in a Windows batch file like one would do in a Unix shell script using getopt/getopts? It doesn't have to be all Posix-y; just something that I can specify what switches I expect, which of them require/allow an argument. They don't need to be "long" switches; single characters will work.

It can be an external .exe that the batch file calls. It has to be freely distributable.

like image 722
Michael Campbell Avatar asked Aug 16 '10 15:08

Michael Campbell


1 Answers

You can you something like this (-h has no args, hence no shift after that, -b and -s take additional args, so shift them).

:GETOPTS
 if /I "%1" == "-h" goto Help
 if /I "%1" == "-b" set BASE=%2 & shift
 if /I "%1" == "-s" set SQL=%2 & shift
 shift
if not "%1" == "" goto GETOPTS
like image 85
Kris Avatar answered Sep 30 '22 20:09

Kris