visual-studio-code - 是否可以将参数传递给 Visual Studio Code 中的任务

标签 visual-studio-code vscode-tasks

这是我的 tasks.json 的一个例子:

{
  "version": "0.1.0",
  "tasks": [
    {
      "taskName": "test",
      "suppressTaskName": true,
      "command": "python",
      "args": [
        "tests/brewer_tests.py"
      ],
      "isTestCommand": true
    }
  ]
}

我可以用 shift+cmd+alt+b 运行它.我也可以用 alt+t 运行它,然后从菜单中选择它。是否可以在该菜单中传递其他参数?例如
enter image description here

你可以像这样将它构建到你的任务中:
{
  "version": "0.1.0",
  "tasks": [
    {
      "taskName": "test",
      "suppressTaskName": true,
      "command": "python",
      "args": [
        "tests/brewer_tests.py",
        $arg1                        # would resolve to "ARG1"
      ],
      "isTestCommand": true
    }
  ]
}

或者类似的东西?

最佳答案

我使用了来自 this answer 的解决方案直到现在,但由于 Visual Studio Code 现在有 official support for task prompts我会在这里添加它作为答案。

在您的 tasks.json 文件中,您添加 key inputs在您的旁边 tasks .该键包含一个包含所有可能参数的数组。请注意,并非每个任务都必须使用所有这些输入。
所有这些输入都有一个 id ,您将使用它来引用任务中的输入。
现在,您只需要在任务中添加 ${input:myInputId}无论您在哪里需要参数。

例子:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Echo param",
            "type": "shell",
            "command": "echo ${input:param1}",
            "problemMatcher": []
        },
        {
            "label": "Echo without param",
            "type": "shell",
            "command": "echo Hello",
            "problemMatcher": []
        },
    ],
    "inputs": [
        {
            "id": "param1",
            "description": "Param1:",
            "default": "Hello",
            "type": "promptString"
        },
    ]
}

任务Echo param将打开一个提示,让你输入一个字符串值,然后它会打印这个值。任务Echo without param将简单地打印“你好”。

关于visual-studio-code - 是否可以将参数传递给 Visual Studio Code 中的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45582698/

相关文章:

android-studio - 在 Android Studio Flutter 项目的上下文菜单中用未显示的列换行

node.js - 运行各种 Node 命令的 VSCode 任务

visual-studio-code - VSCode LiveServer 重新加载问题

terminal - 如何在 Visual Studio Code 终端中使路径可点击

typescript - 为什么不将变量值注入(inject)到模板字符串中?

visual-studio-code - 如何在开始构建时自动清除 VS Code 终端?

arduino - 如何让 Arduino 正常工作而不是在 vscode 上给出 "cannot open source file "avr/pgmspace.h""?

visual-studio-code - 是否可以为 VSCode 中的所有 C++ 文件设置一个 task.json?

visual-studio-code - Visual Studio 代码中的条件任务