Function Windows批处理脚本-反转函数参数
我想向Windows批处理脚本函数传递一组布尔标志。大概是这样的:Function Windows批处理脚本-反转函数参数,function,batch-file,Function,Batch File,我想向Windows批处理脚本函数传递一组布尔标志。大概是这样的: 1 SETLOCAL 2 SET _debug=1 3 SET _x64=1 4 5 call :COMPILE %_debug% %_x64% 6 :: call again where %_debug% is now 0 7 :: call again where %_x64% is now 0 8 :: etc... 9 10 :COMPILE 11 :: stuff depen
1 SETLOCAL
2 SET _debug=1
3 SET _x64=1
4
5 call :COMPILE %_debug% %_x64%
6 :: call again where %_debug% is now 0
7 :: call again where %_x64% is now 0
8 :: etc...
9
10 :COMPILE
11 :: stuff depending on input args
call :COMPILE 0 1
我使用变量\u debug
和\u x64
使调用(第5-7行)更可读,而不是像这样:
1 SETLOCAL
2 SET _debug=1
3 SET _x64=1
4
5 call :COMPILE %_debug% %_x64%
6 :: call again where %_debug% is now 0
7 :: call again where %_x64% is now 0
8 :: etc...
9
10 :COMPILE
11 :: stuff depending on input args
call :COMPILE 0 1
是否有一种简单的方法来传递等价的非变量
?比如:
call :COMPILE ~%_debug% ~%_64%
或
或者我必须声明不包含以下变量:
SET _debug=1
SET _not_debug=0
SET _x64=1
SET _not_x64=0
当我只有这两个变量时,这就足够容易了,但我预期会有更多的变量。
根据初始响应进行编辑: 关于第6行和第7行,我写道:
6 :: call again where %_debug% is now 0
7 :: call again where %_x64% is now 0
我对实际将
\u debug
和\u x64
的值更改为0不感兴趣。我想知道的是,是否有一种方法可以将“not\u var”传递给函数。通过这种方式,我可以保留参数的含义。不幸的是,在任意命令行中无法执行任何类型的(位)算术运算,因此您必须始终将临时变量与以下变量一起使用:
:。。。
调用:编译%\u调试%%\u x64%
设置/A“\u not\u debug=!\u debug,\u not\u x64=!\u x64”
调用:编译%\u非调试%\u非调试64%
:: ...
您可以最小化设置各种变量所需的代码:
for %%V in (debug x64) do set /a _%%V=1, _not_%%V=0
只为两个具有四个值的标志执行上述操作似乎很愚蠢。但是当你添加更多的旗帜时,它变得越来越有吸引力
但是我不明白你为什么不更进一步——为什么不让你的例程期望有意义的文本参数而不是神秘的整数值呢。您的调用不仅会有很好的文档记录,而且例行代码也会更容易遵循。调用方不再需要担心将有意义的文本转换为整数值
如果您发现自己正在开发带有可选参数的复杂例程,那么您应该看看