1

如果您的参数无效,我有一批会显示用法。

例如。

   Running "sample.bat update fuh"

   :usageactions
   set actions=%*
   set toremove=update
   set todisplay=%actions:%toremove% =%
   echo Error: Invalid Arguments - '%todisplay%'.
   echo Type sample.bat %toremove% -h for usage.

预期输出:

  Error: Invalid Arguments - 'fuh'.
  Type sample.bat update -h for usage

但我的输出是:

  Error: Invalid Arguments - 'update'.
  Type sample.bat update -h for usage

如何实现它?任何帮助,将不胜感激。谢谢。

(顺便说一句,如果问题如此混乱,请修改问题。)

4

2 回答 2

3

您尝试在一行中多次扩展表达式,但解析器无法猜测哪些百分比是成对的。

set todisplay=%actions:%toremove% =%

你可以在这里使用延迟扩展

setlocal EnableDelayedExpansion
set toDisplay=!actions:%toRemove% =!

首先 %toRemove% 将被扩展,然后该行看起来像

set toDisplay=!actions:update =!

然后将评估感叹表达式。

于 2012-07-26T13:23:11.373 回答
0

幸运的是,我找到了解决问题的方法,doh。无论如何,这是解决方案。

:usageactions
setLocal EnableDelayedExpansion
set actions=%*
set toremove=update
set todisplay=!actions:%toremove% =!
echo Error: Invalid Arguments - '%todisplay%'.
echo Type sample.bat %toremove% -h for usage.
endlocal

这将显示。

Error: Invalid Arguments - 'fuh'.
Type sample.bat update -h for usage
于 2012-07-26T13:24:58.463 回答