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

式を 1 行で複数回展開しようとしましたが、パーサーはどのパーセントがペアであるかを推測できません。

set todisplay=%actions:%toremove% =%

ここで遅延拡張を使用できます

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

最初の %toRemove% が展開され、次に行は次のようになります

set toDisplay=!actions:update =!

そして、感嘆符が評価されます。

于 2012-07-26T13:23:11.373 に答える
0

幸いなことに、問題の解決策を見つけました。とにかく、ここに解決策があります。

: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 に答える