[英]argparse to validated unknown args in python
test1.py:测试1.py:
_argParserObj = argparse.ArgumentParser()
_argParserObj.add_argument('--tool', type=str, dest='tool')
parsedArgs, passthroughs = _argParserObj.parse_known_args()
When test1.py is executed as below当 test1.py 执行如下
test1.py --tool abc arg1 arg2 --invalid_switch
I would like to show the below error.我想显示以下错误。 But still accept arg1 and arg2 as extra arguments
但仍然接受 arg1 和 arg2 作为额外的 arguments
test1.py: error: unrecognized arguments: -invalid_switch
test1.py:错误:无法识别的 arguments:-invalid_switch
Currently it just parses invalid_switch
also a passthrough and throw any error.目前它只是解析
invalid_switch
也是一个 passthrough 并抛出任何错误。
Can anyone please suggest how to achieve this?谁能建议如何实现这一目标?
You can try parse_args()
instead of parse_known_args()
您可以尝试
parse_args()
而不是parse_known_args()
_argParserObj = argparse.ArgumentParser()
_argParserObj.add_argument('--tool', type=str, dest='tool')
parsedArgs, passthroughs = _argParserObj.parse_args()
parse_args()
will show this error in your case parse_args()
将在您的情况下显示此错误
test.py: error: unrecognized arguments: --invalid_switch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.