debugging VS Code调试提示参数并设置工作目录

l7mqbcuq  于 6个月前  发布在  其他
关注(0)|答案(2)|浏览(147)

我知道如何在launch.json中传递固定参数,例如In Visual Studio Code, how to pass arguments in launch.json。我真正需要的是一个提示符,在那里我可以为改变的参数给予一个值。
另外,我的参数是一个(数据)目录,它有一个非常长的绝对路径。我真的希望能够将工作目录设置为一个包含每个数据目录的路径,所以我只需要提供一个相对目录路径,即目录名。
我正在使用Python,在Windows上(不是我的选择)使用VS Code 1.55.2(也不是我的选择)。

332nm8kg

332nm8kg1#

您可以使用输入变量

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File with arguments",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "args": [
        "--dir",
        "/some/fixed/dir/${input:enterDir}"
      ]
    }
  ],
  "inputs": [
    {
      "id": "enterDir",
      "type": "promptString",
      "description": "Subdirectory to process",
      "default": "data-0034"
    }
  ]
}

字符串
您可以将${input:enterDir}放置在任务"configurations"中的任何字符串中,就像"cwd"属性一样。
如果您想从列表中选择一个目录,因为它是动态的,您可以使用具有pickFile命令的扩展名Command Variable
Command Variable v1.36.0支持fixed文件夹规范。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File with arguments",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "args": [
        "--dir",
        "${input:pickDir}"
      ]
    }
  ],
  "inputs": [
    {
      "id": "pickDir",
      "type": "command",
      "command": "extension.commandvariable.file.pickFile",
      "args": {
        "include": "**/*",
        "display": "fileName",
        "description": "Subdirectory to process",
        "showDirs": true,
        "fromFolder": { "fixed": "/some/fixed/dir" }
      }
    }
  ]
}


在类Unix系统上,你可以将文件夹包含在include glob模式中。在Windows上,你必须使用fromFolder将目录路径转换为可用的glob模式。如果你有多个文件夹,你可以使用predefined属性。

ajsxfq5m

ajsxfq5m2#

虽然前面的答案需要通过名称指定每个命令行参数,但有一种方法可以让VS Code提示整个命令行。这使得解决方案可以与任何PowerShell脚本一起使用,而不是要求launch.json针对您正在调试的每个脚本进行定制。在launch.jsonconfigurations块中,添加:

{
            "type": "PowerShell",
            "request": "launch",
            "name": "PowerShell Launch Current File w/Args Prompt",
            "script": "${file}",
            "args": [
                "${command:SpecifyScriptArgs}"
            ],
            "cwd": "${file}"
        }

字符串
这里的酱料是${command:SpecifyScriptArgs}提示符。

相关问题