【问题标题】:Is it possible to pass arguments to a task in Visual Studio Code是否可以将参数传递给 Visual Studio Code 中的任务
【发布时间】:2018-01-16 20:56:08
【问题描述】:

这是我的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
    }
  ]
}

或者类似的东西?

【问题讨论】:

标签: visual-studio-code vscode-tasks


【解决方案1】:

到目前为止,我一直使用 this answer 的解决方案,但由于 Visual Studio Code 现在有一个 official support for task prompts,我将在此处添加它作为答案。

在您的 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”。

【讨论】:

【解决方案2】:

这就是现在对我有用的东西 - 使用它来运行带有自定义参数的golang sn-p。 如果您为此添加键盘映射,则该过程非常简单。

目前仅在 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
        }
    }

【讨论】:

  • 我一直在寻找这个。这个解决方案似乎工作得很好。您还可以提示输入多个参数,只需像这样链接它们:$arg1 = read-host 'Arg 1:';$arg2 = read-host 'Arg 2:' 到目前为止我发现的唯一缺点是,vs 代码不喜欢 {"value": "", "quoting": "weak" -syntax。
  • @Springrbua 这个应该加到基础任务配置里吧?但是“args”中的“值”字段仍然是不正确的类型。目前尚不清楚如何应用此答案。我有 VS 代码 1.25.1
  • @BenceKaulics 你真的可以把它粘贴到你的任务中。 json 文件。然后将第二个“值”字段的值替换为您要执行的命令。正如我所说,VS 代码会发出警告,但它似乎有效。
【解决方案3】:

关于Input variablesVSCode 1.43 (Feb. 2020)新增功能:

promptString 密码输入

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2020-09-14
    • 2012-12-26
    相关资源
    最近更新 更多