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

查看:105
本文介绍了是否可以将参数传递给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.

在您的task.json文件中,将密钥inputs添加到tasks旁边.此项包含具有所有可能参数的数组.请注意,并非每个任务都必须使用所有这些输入.
所有这些输入都有一个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天全站免登陆