首页 > 解决方案 > 如何调试/启动具有包含“<”和“>”的参数的程序

问题描述

我有一个处理 HTML 内容的程序。使用命令行参数,我可以给它提供测试字符串。

从 Windows 命令行 (cmd.exe) 我可以毫无问题地运行它

C:\Python36\python.exe program.py -t "<head><title>Test</title></head>"

我无法获得可以正确传递此参数的启动配置。

我收到以下错误

< was unexpected at this time.

或者

The system cannot find the file specified.

没有提到它正在寻找的文件

该问题可能是由于所有内容都作为参数传递给 cmd 引号内的事实引起的。

我使用添加了选项的标准Python: Current File配置args

    {
      "name": "Python: Debug with args",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "args" : ["-t","<head><title>Test</title></head>"]
    }

使用单转义添加引号

"args" : ["-t","\"<head><title>Test</title></head>\""]

或双重转义(在 C++ 启动文档中找到)

"args" : ["-t","\\\"<head><title>Test</title></head>\\\""]

没有解决问题。


编辑

部分解决方案,仅适用于新终端

在参数字符串中添加一个空格 ==> VSC 将参数转义为双引号

"args" : ["-t","<head><title>Test</title> </head>"]

我曾经读过一些关于让 VSC 将参数用单引号或双引号括起来的文档。但是我在当前版本中找不到这个文档了。

标签: pythondebuggingvisual-studio-code

解决方案


如果您尝试像这样设置参数怎么办:

"args" : '-t "<head><title>Test</title></head>"'

代替

"args" : ["-t","<head><title>Test</title></head>"]

推荐阅读