首页 > 解决方案 > 在 VS 代码中调试 Python 脚本时出现无法识别的参数错误

问题描述

我正在尝试在 VS 代码中调试以下脚本:

import os
import argparse

def parseArgs():
    parser = argparse.ArgumentParser()

    parser.add_argument("-cn", "--cert_names", nargs="+", default=None, help='Provide one or several cert names to be created')
    parser.add_argument('-pw', '--password', action='store_true', help='Provide a password for private keys and certs')
    args = parser.parse_args()
    
    if not args.cert_names:
        print("Please provide one or more cert names")

def main():
    parseArgs()
    print(args.cert_names)

这是我正在使用的 launch.json 文件:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File and libraries",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode" : false,
            "args": ["-cn cert1 cert2 cert3 cert4"],
            "cwd": "${fileDirname}",
        }
    ]
}

在 VS 代码中运行调试器后,我收到以下消息: create_certs.py: error: unrecognized arguments: -cn cert1 cert2 cert3 cert4

我想要的是将该列表作为参数传递给脚本,我也尝试过使用“args”:[“--cert_names cert1 cert2 cert3 cert4”],结果相同,

有谁知道我哪里出错了?

标签: pythonvisual-studio-codeargparse

解决方案


尝试将 args 分开放置,例如:

"args": ["-cn", "cert1", "cert2", "cert3", "cert4"],

否则,它们都会作为一个巨大的论点通过。


推荐阅读