将多行批处理命令与内联注释相结合

问题描述 投票:0回答:1

我有一个很长的命令行字符串,我想评论一些参数/开关并描述它们的作用以及可以用它们调整什么。以下简化的批处理文件不起作用,但它或多或少是我想要做的:

executable^
 -switch1=3^                   rem should be 3 or 5, three means xxx and 5 means yyy
 -argument1=some-file.txt^
 -file-format=csv^             rem output file format, supported are csv and xml
 -out=result                   rem written file, file extension is copied from file format

我创建了一个简单的脚本来测试将注释与分割命令字符串组合的语法,但我找不到可行的解决方案:

rem WORKS, no space after caret
echo aaa ^
bbb

rem does not work, space after caret
echo aaa ^ 
bbb

rem WORKS space in the next line
echo aaa ^
 bbb
echo.
echo and now with rem-comments
echo.

rem does not work
echo aaa ^rem comment
bbb

rem does not work
echo aaa ^&rem comment
bbb

rem does not work
echo aaa ^ rem comment
bbb

rem does not work
echo aaa ^ &rem comment
bbb
echo.
echo and with double-colon-comments
echo.

:: does not work
echo aaa ^:: comment
bbb

:: does not work
echo aaa ^&:: comment
bbb

:: does not work
echo aaa ^ :: comment
bbb

:: does not work
echo aaa ^ &:: comment
bbb

插入符号之后的任何字符似乎都会破坏它,因此在插入符号之后可能根本不可能有内联注释?有人有想法/解决方案吗?

batch-file comments line-continuation
1个回答
0
投票
不可能在

command 中使用像 rem

 这样的 
commands 分布在多行中。仅当存在下一个回车符(自动读取批处理文件时忽略)和换行符时,插入符字符 ^
 作为行尾
的转义字符才有效。即使像普通空格或水平制表符这样的尾随空白字符也会导致将长命令行分成两个命令行,并且可执行文件的执行将无法按预期进行。 Windows 命令处理器 使用非常古老的语法,它不像现代脚本解释器那样支持空格/制表符。有关处理命令行或批处理文件的更多信息,请参阅:Windows 命令解释器 (CMD.EXE) 如何解析脚本? 解决方案非常简单。长命令行作为一个长命令行写入批处理文件中,并且所有选项都在上面的块中进行了解释,在使用命令

goto

执行时或多或少会被忽略,如下所示。

goto RunExe
rem   -switch1=3 ......... should be 3 or 5, three means xxx and 5 means yyy
rem   -file-format=csv ... output file format, supported are csv and xml
rem   -out=result ........ written file, file extension is copied from file format
:RunExe
executable -switch1=3 -argument1=some-file.txt -file-format=csv

	
© www.soinside.com 2019 - 2024. All rights reserved.