gpt4 book ai didi

windows - 如何在 Batch For 循环中获取 C++ 错误

转载 作者:可可西里 更新时间:2023-11-01 10:06:27 25 4
gpt4 key购买 nike

我正在尝试处理在批处理脚本中编译 C++ 代码时产生的错误。我有一个类似于以下的 for 循环:

@FOR /F "delims=" %%A IN ('COMMAND TO COMPILE C++') DO @(
SET MESSAGE=%%A
SET MESSAGE=!MESSAGE:"='!
CALL :PRINT "!MESSAGE!"
)

我遇到的问题是当我收到有关模板、比较运算符、异或运算符或位移运算符的错误时。在这些情况下,%%A 中的错误字符串包含引号和大于/小于符号。例如:

error: yada yada TemplateClass<> "Some Message"
error: yada yada operator <<() 'Some Message'
error: yada yada 'operator ^()' "Some Message"

我的问题是:是否可以在 for 循环中捕获包含双引号、单引号和小于/大于字符的整个消息?我尝试过的:

:: Fails when %%A contains a quote
SET "MESSAGE=%%A"
:: Fails when %%A contains a quote
SET MESSAGE="%%A"
:: Fails when %%A contains a greater-than symbol
SET MESSAGE=%%A

有没有办法让我在另一个变量中捕获 %%A,这样我就可以在批处理文件的其他地方使用这个变量?谢谢。

最佳答案

为变量赋值的“正确”方法是set "message=%%a"。但是在调用 print 函数时,不是传递值,而是传递变量名(引用),并在函数内部取消引用变量。

并且,在您的 :print 函数中,当访问变量的内容时使用 !var! 符号来避免将特殊字符视为命令的一部分而不是文本的一部分。

一个小样本(dbenham 风格)

@echo off

setlocal enableextensions

for /f "tokens=* delims=:" %%f in ('findstr /b /c:":::" "%~f0" ') do (
set "line=%%f"
call :print line
)

exit /b

:print variable
setlocal enabledelayedexpansion
echo( Parameter : %~1
echo( Dereferenced value : !%~1!
set "var=!%~1!"
echo( Variable with the value : !var!

echo.

rem If var contains special characters the following line fails
rem echo( Variable with the value : %var%

endlocal
goto :EOF


exit /b

:::This is a plain line
:::This line has special characters !"'#$%&/()=?:
:::This line has more special characters << " >>
:::"This is a double quoted line"
:::'This is a single quoted line'

关于windows - 如何在 Batch For 循环中获取 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20414104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com