简体   繁体   English

在Windows批处理文件中寻找Unix风格的'getopt'命令行解析

[英]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? 谁能帮助我在Windows批处理文件中找到一些解析命令行args的东西,就像使用getopt / getopts在Unix shell脚本中一样? It doesn't have to be all Posix-y; 不必全是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. 它可以是批处理文件调用的外部.exe。 It has to be freely distributable. 它必须可以自由分发。

You can you something like this (-h has no args, hence no shift after that, -b and -s take additional args, so shift them). 您可以使用类似的方法(-h没有args,因此之后没有移位,-b和-s需要附加的args,因此进行移位)。

: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

There is no such thing as getopt/getopts-like parsing of commandline arguments as you know from Unix/Linux. 从Unix / Linux可以知道,没有像命令行命令行参数这样的getopt / getopts解析方法。

Batch files only know about %0 , %1 , %2 , ... and %* (and such variations as %~0 , %~1 ... which remove quotes, should there be ones around an arg). 批处理文件只知道%0%1%2 ,...和%* (以及诸如%~0%~1 ...之类的变体,如果有arg的话,它们会删除引号)。

Up to nine arguments. 最多九个参数。 If there are more to process, you can use shift (equiv. to shift /1 if enableextensions happened) to remove the first arg and shift the rest. 如果还有更多要处理的内容,则可以使用shift (如果发生enableextensionsenableextensionsshift /1 )来删除第一个arg并转移其余的arg。

Basically that's it. 基本上就是这样。


(Maybe you should explain more what exactly you are trying to achieve with the batch, why you must use batch, and what your other external constraints are.) (也许您应该更多地解释要使用批处理实现的目标,为什么必须使用批处理以及其他外部约束是什么。)

看一下: https : //github.com/frossm/getopt.btm-这是我见过的最完整的实现之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM