Python argparse:有没有办法在 nargs 中指定范围?
我有一个可选参数,它支持参数列表本身。
我的意思是,它应该支持:
- -f 1 2
- -f 1 2 3
但不支持:
- -f 1
- -f 1 2 3 4
有没有办法在 argparse 中强制执行此操作?现在我使用 nargs="*",然后检查列表长度。
编辑:根据要求,我需要的是能够定义一系列可接受的参数数量。我的意思是,(在示例中)2 或 3 个参数是正确的,但 1 或 4 或任何不在 2..3 范围内的参数是正确的
I have an optional argument that supports a list of arguments itself.
I mean, it should support:
- -f 1 2
- -f 1 2 3
but not:
- -f 1
- -f 1 2 3 4
Is there a way to force this within argparse ? Now I'm using nargs="*", and then checking the list length.
Edit: As requested, what I needed is being able to define a range of acceptable number of arguments. I mean, saying (in the example) 2 or 3 args is right, but not 1 or 4 or anything that's not inside the range 2..3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自定义操作来执行此操作:
You could do this with a custom action: