首页 > 解决方案 > 将输入字符串映射到 launch.json 文件中的数值

问题描述

我有以下 launch.json 文件:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Service1",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "127.0.0.1",
                "port": "${input:envType}"
            },
            ... 
        },
        ...
    ],
    "inputs": [
        {
            "type": "pickString",
            "id": "envType",
            "description": "Which env do you want to debug?",
            "options": [
              "development",
              "staging",
              "live",
            ],
            "default": "development"
          },
    ]

我想要实现的是:将特定环境映射到端口。例如,我希望如果用户选择“开发”环境,将在端口字段中打印的值是 5000。

这可能吗?

标签: visual-studio-codeconfigremote-debugging

解决方案


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

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Service1",
      "type": "python",
      "request": "attach",
      "connect": {
        "host": "127.0.0.1",
        "port": "${input:envType}"
      }
    }
  ],
  "inputs": [
    {
      "id": "envType",
      "type": "command",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "description": "Which env do you want to debug?",
        "options": [
          ["development", "5000"],
          ["staging", "5100"],
          ["live", "5200"]
        ],
        "default": "5000"
      }
    }
  ]
}

推荐阅读