为什么我的argparse系统出现系统出口2错误?

Sar*_*rah 6 python cpu-word argparse

这是我编码的主要部分.当我运行我的整个代码集时,它显示在此段中发生了异常:"options = parse.parse_args()"中的SystemExit2错误.我可以知道这里出了什么问题吗?

import argparse
import queue
import roypy
from sample_camera_info import print_camera_info
from roypy_sample_utils import CameraOpener, add_camera_opener_options
from roypy_platform_utils import PlatformHelper

class MyListener (roypy.IRecordStopListener):
   """A simple listener, in which waitForStop() blocks until onRecordingStopped has been called."""
   def __init__ (self):
       super (MyListener, self).__init__()
       self.queue = queue.Queue()

   def onRecordingStopped (self, frameCount):
       self.queue.put (frameCount)

   def waitForStop (self):
       frameCount = self.queue.get()
       print ("Stopped after capturing {frameCount} frames".format (frameCount=frameCount))

def main ():
    platformhelper = PlatformHelper() 
    parser = argparse.ArgumentParser (usage = __doc__)
    add_camera_opener_options (parser)
    parser.add_argument ("--frames", type=int, required=True, help="duration to capture data (number of frames)")
    parser.add_argument ("--output", type=str, required=True, help="filename to record to")
    parser.add_argument ("--skipFrames", type=int, default=0, help="frameSkip argument for the API method")
    parser.add_argument ("--skipMilliseconds", type=int, default=0, help="msSkip argument for the API method")
    options = parser.parse_args()

    opener = CameraOpener (options)
    cam = opener.open_camera ()

    print_camera_info (cam)

    l = MyListener()
    cam.registerRecordListener(l)
    cam.startCapture()
    cam.startRecording (options.output, options.frames, options.skipFrames, options.skipMilliseconds)

    seconds = options.frames * (options.skipFrames + 1) / cam.getFrameRate()
    if options.skipMilliseconds:
        timeForSkipping = options.frames * options.skipMilliseconds / 1000
        seconds = int (max (seconds, timeForSkipping))

    print ("Capturing with the camera running at {rate} frames per second".format (rate=cam.getFrameRate()))
    print ("This is expected to take around {seconds} seconds".format (seconds=seconds))

    l.waitForStop()

    cam.stopCapture()

   if (__name__ == "__main__"):
       main()
Run Code Online (Sandbox Code Playgroud)

这是我执行的追溯:

发生异常:SystemExit 2

文件"C:\ Users\NPStudent\Desktop\Python Code\sample_record_rrf.py",第44行,主要选项= parser.parse_args()

在main()中的文件"C:\ Users\NPStudent\Desktop\Python Code\sample_record_rrf.py",第69行

这是我运行程序时的命令行: 在此输入图像描述

And*_*asT 1

您有指定为必需 ( required=True) 的参数,但这些参数丢失了!您发布的图片清楚地表明了这一点。

在 VSCode 中按 F5 也将在不带参数的情况下运行它。产生相同的错误,进而导致 VSCode 抱怨您的脚本崩溃了。

您必须使用参数--frames和来调用您的程序--output。首先在命令行上尝试,然后为 VSCode创建启动配置。