python - 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,但是当我在编辑器中切换这些文件时,我必须在这些配置之间手动切换。)
解决方案
使用扩展命令变量,您可以根据文件路径选择参数。
{
"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": ""
}
}
]
推荐阅读
- cron - 多个自定义 crontab 文件到一个主 crontab
- azure-aks - AKS 节点池升级失败,Azure 活动日志“操作超时,请重试”。
- python - IndexError:索引 6 超出轴 0 的范围,大小为 6
- android - 在 Android Webview 的 navigator.geolocation.getCurrentPosition() 中出现“位置信息不可用”错误
- c++ - 非 POD 的零初始化
- angular - 为什么在 Angular 中,在主组件 css 文件中导入全局 css 文件而不是在 styles.css 中导入它们之间存在很大差异
- kotlin - 你能定义 Kotlin Annotations 的替代简写吗?
- sql-server - SQL Server 降级到 2014 的权限问题 Windows 10
- magento - 为以编程方式创建的商店视图自动创建 url 重写
- javascript - 制作通过php解析javascript请求的路由器