51

这是我的一个例子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,然后从菜单中选择它。是否可以在该菜单中传递其他参数?例如 在此处输入图像描述

您可以像这样将其构建到您的任务中:

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

或者类似的东西?

4

3 回答 3

63

到目前为止,我一直使用此答案中的解决方案,但由于 Visual Studio Code 现在正式支持任务提示,我将在此处将其添加为答案。

在您的 tasks.json 文件中,您将密钥添加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将简单地打印“Hello”。

于 2018-12-19T07:38:13.363 回答
5

这是目前对我有用的方法-使用它来运行golang带有自定义参数的代码段。如果您为此添加键盘映射,则该过程非常简单。

到目前为止,仅在 Windows 下对此进行了测试 - linux 版本因此被注释掉

{
        "label": "runwithargs",
        "type": "shell",
        "windows": {
            "options": {
                "shell": {
                    "executable": "powershell.exe",
                    "args": [
                        "-NoProfile",
                        "-ExecutionPolicy",
                        "Bypass",
                        "-Command"
                    ]
                }
            },
            "command": "",
            "args": [
                { "value": "$cmdargs = read-host 'Enter command line arguments';", "quoting": "weak"},
                { "value": "go run ${file} $cmdargs", "quoting": "weak"}
            ]
        },
        /*"linux": {
            "command": "echo 'Enter command line arguments: '; read cmdargs;",
            "args": [ "go run ${file} $cmdargs" ]                
        },*/          
        "presentation": {
            "panel": "dedicated",
            "focus": true
        }
    }
于 2018-06-20T15:39:05.940 回答
5

关于Input variablesVSCode 1.43(2020 年 2 月)增加了一个新特性:

promptString 密码输入

" promptString"" input" 类型可以有"password": true,这会导致显示的快速输入像密码一样掩盖键入的内容

于 2020-02-28T18:23:40.340 回答