Python argparse:是否可以在nargs中指定范围? [英] Python argparse: Is there a way to specify a range in nargs?

查看:343
本文介绍了Python argparse:是否可以在nargs中指定范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可选参数,它本身支持参数列表.

I have an optional argument that supports a list of arguments itself.

我的意思是,它应该支持:

I mean, it should support:

  • -f 1 2
  • -f 1 2 3

但不是:

  • -f 1
  • -f 1 2 3 4

是否有办法在argparse中强制执行此操作?现在,我使用nargs ="*",然后检查列表长度.

Is there a way to force this within argparse ? Now I'm using nargs="*", and then checking the list length.

根据要求,我需要能够定义可接受数量的参数.我的意思是说(在示例中)2或3个args是正确的,但不是1或4或不在2..3范围内的任何东西

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

推荐答案

您可以使用

这篇关于Python argparse:是否可以在nargs中指定范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆