首页 > 解决方案 > 如何在 YAML-CPP 中将版本字符串写为文字(不是字符串)?

问题描述

我正在尝试编写以下信息:

hints:
  SoftwareRequirement:
    packages:
      ApplicationName:
        version: [ 1.7.3.nonRelease ]

我正在使用以下代码部分:

std::string m_exeName = # I get this from my CMakeLists file
std::string versionID = # I get this from my CMakeLists file

YAML::Node hints = config["hints"];
config["hints"]["SoftwareRequirement"]["packages"][m_exeName]["version"] = "[ " + versionID + " ]";

它让我得到以下信息:

hints:
  SoftwareRequirement:
    packages:
      ApplicationName:
        version: "[ 1.7.3.nonRelease ]"

有没有办法让方括号内的引号或完全删除它们?这符合通用工作流语言(CWL) 标准。

可能与这个问题有关

编辑(从答案添加结果):

带着这个去:

config["hints"]["SoftwareRequirement"]["packages"][m_exeName]["version"][0] = versionID

结果:

hints:
  SoftwareRequirement:
    packages:
      ApplicationName:
        version: 
          - 1.7.3.nonRelease

With 是一个有效的 CWL。

标签: c++yamlyaml-cpp

解决方案


[]是序列的 YAML 语法;所以如果你想写

[ 1.7.3.nonRelease ]

那么你正试图用一个元素编写一个序列1.7.3.nonRelease。当您告诉 yaml-cpp 写入 string[ 1.7.3.nonRelease ]时,它注意到如果它只是直接粘贴文本,它将被解释为一个列表,因此它引用了该字符串以防止这种情况发生。

如果你真的想写一个包含一个元素的列表,那就这样吧:

config["hints"]["SoftwareRequirement"]["packages"][m_exeName]["version"][0] = versionID;

推荐阅读