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

Luk*_*ney 22 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
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我可以用它来运行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
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

或类似的东西?

Spr*_*bua 18

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

在您的task.json文件中,将密钥添加inputstasks。此项包含具有所有可能参数的数组。请注意,并非每个任务都必须使用所有这些输入。
所有这些输入都有一个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"
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

该任务Echo param将打开一个提示,让您输入一个字符串值,然后将打印该值。该任务Echo without param将仅打印“ Hello”。

  • 官方文档 https://code.visualstudio.com/docs/editor/variables-reference#_input-variables (3认同)

bus*_*hed 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
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • @BenceKaulics 你真的可以把它粘贴到你的任务中。Json 文件。然后用您要执行的命令替换第二个“值”字段的值。正如我所说,VS 代码给出了警告,但它似乎有效。 (2认同)

Von*_*onC 5

关于Input variablesVSCode 1.43(2020 年 2 月)添加了一个新功能:

promptString 密码输入

在“ promptString”“ input”类型可以有"password": true,这将导致快速输入昭示着掩盖类似于密码键入的内容