首页 > 解决方案 > 在 vscode 任务中处理来自输入用户的多个值

问题描述

然后根据选择的选项在inputsvscode中接收用户输入以处理多个值的最佳解决方案是什么?tasks.json

有没有像下面这样的解决方案?

{
    "version": "2.0.0",
    "inputs": [
        {
            "id": "selectProject",
            "type": "pickString",
            "description": "Some decription",
            "options": 
            [
                { 
                    "label": "project1", 
                    "value" : {"path":"","name":"","link":"","anyOther":""}  // something like this
                } ,
                { 
                    "label": "project2", 
                    "value" : {"path":"","name":"","link":"","anyOther":""}  // something like this
                }
            ]
        }
    ],
    "tasks": [
        {
            "label": "test on projects",
            "type": "process",
            "command": "echo",
            "args": [
                "${input:selectProject.path}", // handle many info from one chosen option
                "${input:selectProject.name}",
                 "${input:selectProject.link}",
                 "${input:selectProject.anyOther}"
            ]
        }
    ]
}

当前选项只能接收一个值:(

 "options": 
            [
                { "label": "project1", "value" : "value"  } ,
                { "label": "project2", "value" : "value"  }
            ]

标签: visual-studio-codevscode-settingsvscode-tasks

解决方案


您可以使用扩展命令变量v1.22.0。

该命令extension.commandvariable.pickStringRemember可以记住 1 个选择的多个值。

label-value 的语法有点不同,因为我不知道pickString支持它。

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Do some project",
      "type": "process",
      "command": "echo",
      "args": [
        "${input:selectProject.path}",
        "${input:selectProject.name}",
        "${input:selectProject.link}",
        "${input:selectProject.anyOther}"
      ],
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "selectProject.path",
      "type": "command",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "key": "path",
        "options": [
          ["project1", {"path":"p1","name":"n1","link":"lnk1","anyOther":"any1"}],
          ["project2", {"path":"p2","name":"n2","link":"lnk2","anyOther":"any2"}]
         ],
        "description": "Pick a project"
      }
    },
    {
      "id": "selectProject.name",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "name" }
    },
    {
      "id": "selectProject.link",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "link" }
    },
    {
      "id": "selectProject.anyOther",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "anyOther" }
    }
  ]
}

推荐阅读