ArgumentParse.ArgumentParser引发ArgumentE

2024-05-15 16:56:33 发布

您现在位置:Python中文网/ 问答频道 /正文

conflict_handler(action, confl_optionals)
  File "/usr/local/lib/python3.6/argparse.py", line 1510, in _handle_conflict_error
    raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument -h/--height: conflicting option string: -h

以上是错误信息, 这是我的密码, 我看不出错误:

#1)解析参数

^{pr2}$

Tags: inpystringlibusrlocallineargparse
2条回答

选项“-h”默认预定义为“help”选项,它打印描述和参数列表。您的自定义“-h height”与此冲突,从而导致错误。在

覆盖默认的“-h help”选项不是很好,因为许多用户希望“-h”选项打印帮助消息。(所以如果我是你,我会找到另一种方法来命名这个选项。)但是如果你真的需要的话,你可以忽略它,方法是在构造函数中使用^{} parameter。像这样:

parser = argparse.ArgumentParser(description="Description for my parser", add_help=False)

如果您想保留“help”选项,您必须添加另一行parser.add_argument(" help", action="help")。(感谢chepner

错误提示您使用的是与其他参数冲突的参数名。尤其是在这个例子中-h选项。lib argparse始终包含-h选项以打印脚本帮助,因此对于高度,必须使用与-h不同的参数,例如-ht。在

parser = argparse.ArgumentParser(description="Description for my parser")
parser.add_argument("-v", " velocity", action="store", required=True, help="The velocity of the object is required")
parser.add_argument("-a", " angle", action="store", type=float, required=True, help="The angle of the object is required")
parser.add_argument("-ht", " height", required=False, default= 1.2, help="The height of the object is not required. Default is set to 1.2 meters" )

相关问题 更多 >