【问题标题】:How to fix "error: the following arguments are required: -i/--image"如何修复“错误:需要以下参数:-i/--image”
【发布时间】:2019-08-27 02:47:48
【问题描述】:

我在看别人的代码,跟着python代码;

import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the input image")
args = vars(ap.parse_args())

在最后一行给出以下错误;

usage: sample.py [-h] -i IMAGE
sample.py: error: the following arguments are required: -i/--image

我该如何解决这个问题?到目前为止,我尝试过的任何方法似乎都没有帮助。

【问题讨论】:

  • 您是否尝试提供它要求的参数?
  • 我不知道该怎么做。您的意思是输入占位符导致了问题?

标签: python-3.x argparse


【解决方案1】:

运行sample.py时,需要指定-i/--image参数:

python sample.py --image image/cat.png

如果您希望 image 参数是可选的,请删除 required=True

ap.add_argument("-i", "--image", help="path to the input image")

【讨论】:

  • 我不知道你可以/应该这样做。谢谢。
猜你喜欢
  • 2021-07-12
  • 2021-12-18
  • 2020-09-30
  • 1970-01-01
  • 2019-05-26
  • 2018-12-12
  • 1970-01-01
  • 2023-02-09
  • 2019-11-09
相关资源
最近更新 更多