如何将输入变量从 launch.json 传递到 vscode 中的 tasks.json

How to pass input variable from launch.json to tasks.json in vscode

我可以在 launch.json 中使用来自 launch.json 输入变量

"configurations": [
  {
    ...
    "args": [${input:file_no}]
    "preLanuchTask": "runPreTasks"
    ...
  }
],
"inputs": [
  {
    "id": "file_no",
    "type": "promptString"
  }
]

现在,我想在 tasks.json.

中无需再次输入即可访问同一个变量
{
  "version": "2.0.0",
  "tasks":[
    {
      "label": "runPreTasks",
      "type": "shell",
      "command": sh,
      "args": [
        "/path2script/scriptName.sh",
        "${input:file_no}"    // This does not work, without defining input again
      ]
    }
  ]
}

有没有办法将输入变量从 launch.json 传递到 vscode 中的 tasks.json

您可以使用扩展 Command Variable v1.21.0

它有一个命令 extension.commandvariable.promptStringRemember,其行为与 ${input:name} promptString 变量相同。通过添加 key 属性 结果保存在此键下,您可以使用 extension.commandvariable.rememberPick 命令检索它。

extension.commandvariable.rememberPick 命令可以在不同于 extension.commandvariable.promptStringRemember 命令的 task/launch 中使用

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Task 1",
      "type": "shell",
      "command": "dostuff1",
      "args": ["-p", "${input:promptPath}"]
    },
    {
      "label": "Task 2",
      "type": "shell",
      "command": "dostuff2",
      "args": ["-p", "${input:rememberPath}"]
    },
    {
      "label": "Do Task 1 and 2",
      "dependsOrder": "sequence",
      "dependsOn": ["Task 1", "Task 2"],
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "promptPath",
      "type": "command",
      "command": "extension.commandvariable.promptStringRemember",
      "args": {
        "key": "path",
        "description": "Enter a path"
      }
    },
    {
      "id": "rememberPath",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "path" }
    }
  ]
}

根据@rioV8 的回答,我编辑了我的 json 文件,如下所示:

launch.json:

"configurations": [
  {
    ...
    "args": [${input:file_no}]
    "preLanuchTask": "runPreTasks"
    ...
  }
],
"inputs": [
  {
    "id": "file_no",
    "type": "command",
    "command": "extension.commandvariable.promptStringRemember",
    "args": {
      "key": "lastnumber",
      "description": "Enter the number"
    }
  }
]

tasks.json:

{
  "version": "2.0.0",
  "tasks":[
    {
      "label": "runPreTasks",
      "type": "shell",
      "command": sh,
      "args": [
        "/path2script/scriptName.sh",
        "${input:file_no}"    
      ]
    }
  ]
  "inputs": [
    {
      "id": "file_no",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "lastnumber" }
    }
  ]
}