Python argparse:是否可以在nargs中指定范围?

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

您可以使用