如何从launch.json传递带有特殊字符的参数?

Uni*_*LSD 3 python visual-studio-code

我正在尝试通过 launch.json 将参数传递给我的 Python 程序,我的参数之一需要特殊字符,因为它是密码(我计划添加更安全的方式来输入密码,但这不是重点)。

这是我的launch.json(密码已更改但仍带有特殊字符)

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "args": [
                "-u",
                "camera@iot-project.com",
                "-p",
                "Y^bKKiUPu!fM6!dBsvnALmuXbP6fqT$d"
            ],
            "console": "integratedTerminal"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

当我将密码参数设置为 时"'Y^bKKiUPu!fM6!dBsvnALmuXbP6fqT$d'",它实际上将单引号传递到 Python 程序中,这不是我想要的(在带有单引号的终端中运行程序有效)。

这是我的 Python 程序:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "args": [
                "-u",
                "camera@iot-project.com",
                "-p",
                "Y^bKKiUPu!fM6!dBsvnALmuXbP6fqT$d"
            ],
            "console": "integratedTerminal"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Gin*_*pin 8

对于 Python 扩展和console配置"integratedTerminal",您可以使用"internalConsole".

{
    "name": "run-py-with-special-chars-internalconsole",
    "type": "python",
    "request": "launch",
    "cwd": "${workspaceFolder}/Q",
    "program": "${workspaceFolder}/Q/test.py",
    "args": [
        "-u",
        "camera@iot-project.com",
        "-p",
        "Y^bKKiUPu!fM6!dBsvnALmuXbP6fqT$d"
    ],
    "console": "internalConsole"  // <--------------------
}
Run Code Online (Sandbox Code Playgroud)

这是您的代码和print-ed 输出到调试控制台选项卡的精简版本:

vs 代码调试控制台

保存密码的地方,与手动运行脚本相同:

{
    "name": "run-py-with-special-chars-internalconsole",
    "type": "python",
    "request": "launch",
    "cwd": "${workspaceFolder}/Q",
    "program": "${workspaceFolder}/Q/test.py",
    "args": [
        "-u",
        "camera@iot-project.com",
        "-p",
        "Y^bKKiUPu!fM6!dBsvnALmuXbP6fqT$d"
    ],
    "console": "internalConsole"  // <--------------------
}
Run Code Online (Sandbox Code Playgroud)

internalConsole选项使用 VS Code 的调试器控制台。在写这个答案时,我试图为它找到一个很好的参考,但我发现的只是:Debug Console,它说“可以在调试控制台 REPL 中评估表达式”。

这似乎意味着与integratedTerminal. 在 Linux 和 Mac 上,这可能意味着bash,而 an! 在终端上意味着不同的东西

integratedTerminal当您查看终端输出时,您可以看到该选项的问题

bash-3.2$  env DEBUGPY_LAUNCHER_PORT=56036 /path/to/bin/python 
           /path/to/.vscode/extensions/ms-python.python-2020.3.69010/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher 
           /path/to/test.py -u camera@iot-project.com -p Y^bKKiUPu!fM6!dBsvnALmuXbP6fqT$d 
bash: !fM6!dBsvnALmuXbP6fqT$d: event not found
Run Code Online (Sandbox Code Playgroud)

并将带有特殊字符的密码作为未加引号传递。