是否可以将参数传递给 Visual Studio Code 中的任务 [英] Is it possible to pass arguments to a task in Visual Studio Code

查看:42
本文介绍了是否可以将参数传递给 Visual Studio Code 中的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 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 运行它,然后从菜单中选择它.是否可以在该菜单中传递其他参数?例如

I can run this with shift+cmd+alt+b. I can also run it with alt+t, and choose it from the menu. Is it possible to pass additional arguments in that menu? e.g.

你可以像这样将它构建到你的任务中:

And you could build it into your task like so:

{
  "version": "0.1.0",
  "tasks": [
    {
      "taskName": "test",
      "suppressTaskName": true,
      "command": "python",
      "args": [
        "tests/brewer_tests.py",
        $arg1                        # would resolve to "ARG1"
      ],
      "isTestCommand": true
    }
  ]
}

或者类似的东西?

推荐答案

我使用了从 这个答案 到现在的解决方案,但由于 Visual Studio Code 现在有官方支持任务提示 我会在这里添加它作为答案.

I used the solution from this answer until now, but since Visual Studio Code has now an official support for task prompts I will add it as an answer here.

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

In your tasks.json file, you add the key inputs next to your tasks. This key contains an array with all possible parameters. Note that not every task has to use all of these inputs.
All of these inputs have an id, which you will use to reference the input in your task.
Now, in the task you only need to add ${input:myInputId} whereever you need the parameter.

示例:

{
    "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 将简单地打印Hello".

The task Echo param will open a prompt, which lets you input a string value and it will then print this value. The task Echo without param will simply print "Hello".

这篇关于是否可以将参数传递给 Visual Studio Code 中的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆