Python argparse type and choice restrictions with nargs > 1

后端 未结 4 2100
自闭症患者
自闭症患者 2020-12-30 02:09

The title pretty much says it all. If I have nargs greater than 1, is there any way I can set restrictions (such as choice/type) on the individual args parsed?

This

4条回答
  •  抹茶落季
    2020-12-30 02:29

    A caller of a Action class only catch a ArgumentError.

    https://github.com/python/cpython/blob/3.8/Lib/argparse.py#L1805

    For expecting to catch an exception by a caller, You should raise as following in your custom action.

    raise ArgumentError(self, 'invalid subject {s!r}'.format(s=subject))

提交回复
热议问题