【问题标题】:Retrieve Variable from Batch Task and Use It in launch.json in VS Code从批处理任务中检索变量并在 VS Code 的 launch.json 中使用它
【发布时间】:2020-10-06 01:55:34
【问题描述】:

我有一个批处理文件,它使用以下内容设置变量:

set "MyVariable=Test"

我在 tasks.json 中配置了一个任务来运行这个批处理文件:

{
    "version": "2.0.0",
    "tasks": [{
        "label": "VariableTest",
        "command": "${workspaceFolder}/.vscode/VariableTest.bat",
        "args": [],
        "type": "shell"
    }]
}

如何从我的 launch.json 文件中检索此变量的值? "VariableTest" 设置为启动配置的 preLaunchTask,此配置中的参数取决于此变量。

我当然在运行 Windows (10)。

谢谢!

【问题讨论】:

  • 您可以从您的第一个批处理文件创建另一个批处理文件,然后使用 "dependsOn": ["firstTask"] 选项在另一个相关任务中执行该批处理文件

标签: windows batch-file visual-studio-code


【解决方案1】:

这个变量被设置在一个单独的shell中。

您需要将值存储在文件中并使用扩展名来检索内容。

使用

echo SomeText > c:\temp\VariableResult.txt

将变量值存储在文件中。

您可以使用Command Variable 和命令extension.commandvariable.file.content

一个例子launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File Args",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "args" : ["${input:variableResult}"],
      "preLaunchTask": "VariableTest"
    }
  ],
  "inputs": [
    {
      "id": "variableResult",
      "type": "command",
      "command": "extension.commandvariable.file.content",
      "args": {
        "fileName": "c:\\temp\\VariableResult.txt"
      }
    }
  ]
}

如果输出文件包含键值对,您可以specify the key to use

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2021-07-03
    • 2020-10-07
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多