首页 > 解决方案 > Python argparse 忽略默认值

问题描述

我正在尝试使用defualtargparse 的参数,但它似乎不起作用:

ap = argparse.ArgumentParser()

predictor_path = r"C:\Noam\Code\vision_course\shape_predictor\shape_predictor_68_face_landmarks.dat"
assert os.path.isfile(predictor_path)
ap.add_argument("-p", "--shape-predictor",
                required=True,
                default=predictor_path,
                help="path to facial landmark predictor",
                nargs='?',
                )
image_path = r"C:\Noam\Code\vision_course\face_pose_estimation\images\valid_set\images\image_00008.png"
assert os.path.isfile(image_path)
ap.add_argument("-i", "--image",
                required=True,
                default=image_path,
                help="path to input image",
                nargs='?',
                )

在没有命令行参数的情况下调用时会出现以下错误:

错误:需要以下参数:-p/--shape-predictor, -i/--image

由于我提供了一个默认值,我希望它可以在没有参数的情况下工作。

有任何想法吗?

标签: pythonargparse

解决方案


如果您希望在未指定选项时使用默认值,请删除required=True. 因为您已经包含了,所以required=True您说该选项必须出现在命令行上


推荐阅读