在VS代码中调试Python脚本时出现无法识别的参数错误

2024-10-02 04:32:14 发布

您现在位置: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:错误:无法识别的参数:-cn cert1 cert2 cert3 cert4

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

有人知道我哪里出错了吗


Tags: 代码import脚本parsercertnamesdefargparse

热门问题