首页 > 解决方案 > launch.json - 默认情况下不同程序的不同参数?

问题描述

我正在使用 Visual Studio Code 来编辑和测试 python。我通常在 Code 中打开整个文件夹,并且文件夹中有多个 python 文件。当我在编辑器中打开一个特定的 Python 文件并按 F5 开始调试时,我希望传递给我正在调试的程序的默认参数会根据我正在调试的文件而有所不同。例如 p1.py 的一组参数和 p2.py 的一组不同的参数,但无需切换调试配置。那可能吗?它似乎与如何在 Visual Studio Code 的同一项目文件夹中设置多个 launch.json 或不同参数有关?但不完全是我想要的。我还查看了https://code.visualstudio.com/docs/editor/debugging#_launch-configurations如果我没看错,那我就不走运了。(我可以创建两个配置,一个用于 p1.py,另一个用于 p2.py,但是当我在编辑器中切换这些文件时,我必须在这些配置之间手动切换。)

标签: pythonvisual-studio-code

解决方案


使用扩展命令变量,您可以根据文件路径选择参数。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "args" : ["${input:arg1}","${input:arg2}"],
      "console": "integratedTerminal"
    }
  ],
  "inputs": [
    {
      "id": "arg1",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "/p1.py": "foobar",
        "/p2.py": "blabla"
      }
    },
    {
      "id": "arg1",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "/p1.py": "tralala",
        "/p2.py": ""
      }
    }
  ]


推荐阅读