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
Run Code Online (Sandbox Code Playgroud)
上面是错误信息,这是我的代码,我没有看到错误:
# 1) 解析参数
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("-h", "--height", required=False, default= 1.2, help="The height of the object is not required. Default is set to 1.2 meters" )
Run Code Online (Sandbox Code Playgroud)
选项“-h”默认预定义为“帮助”选项,用于打印描述和参数列表。您的自定义“-h --height”与此冲突,从而导致错误。
覆盖默认的“-h --help”选项不是很好,因为许多用户希望“-h”选项打印帮助消息。(所以如果我是你,我会找到另一种方式来命名选项。)但是如果你真的需要通过在构造函数中使用add_help
参数,你可以忽略它。像这样:
parser = argparse.ArgumentParser(description="Description for my parser", add_help=False)
Run Code Online (Sandbox Code Playgroud)
如果要保留“--help”选项,则必须添加另一行parser.add_argument("--help", action="help")
. (感谢chepner)
小智 0
正如错误表明您使用的参数名称与其他名称冲突。特别是在这种情况下 -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" )
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1629 次 |
最近记录: |