停止解析第一个未知参数
使用 argparse
是否可以停止解析参数在第一个未知的论点?
我找到了两个几乎解决方案;
parse_known_args
,但是这个允许在第一个未知参数之后检测已知参数。nargs=argparse.REMAINDER
,但这不会停止解析,直到第一个非选项参数。在此之前的任何无法识别的选项都会生成错误。
我是不是忽略了什么?我应该使用 argparse 吗?
Using argparse
, is it possible to stop parsing arguments at the first unknown argument?
I've found 2 almost solutions;
parse_known_args
, but this allows for known parameters to be detected after the first unknown argument.nargs=argparse.REMAINDER
, but this won't stop parsing until the first non-option argument. Any options preceding this that aren't recognised generate an error.
Have I overlooked something? Should I be using argparse
at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己没有使用过 argparse(需要保持我的代码与 2.6 兼容),但是浏览文档,我认为您没有错过任何内容。
所以我想知道为什么你希望 argparse 停止解析参数,以及为什么
--
伪参数不能完成这项工作。 来自文档:I haven't used
argparse
myself (need to keep my code 2.6-compatible), but looking through the docs, I don't think you've missed anything.So I have to wonder why you want
argparse
to stop parsing arguments, and why the--
pseudo-argument won't do the job. From the docs:一种方法是使用
getopt
来代替,尽管它可能并不适合所有情况。例如:
一旦 getopt 遇到非选项参数,它将停止处理参数。
One way to do it, although it may not be perfect in all situations, is to use
getopt
instead.for example:
Once
getopt
encounters a non-option argument it will stop processing arguments.