Catching ArgumentTypeError exception from custom action

后端 未结 1 1382
梦谈多话
梦谈多话 2021-01-04 20:52

What is the best practice to throw an ArgumentTypeError exception from my own custom action and let the argparse to catch it for me?

It seems that argparse\'s try/ex

1条回答
  •  孤街浪徒
    2021-01-04 21:20

    After looking at argparse source code I figured out that it translates ArgumentTypeError to ArgumentError exception.

    So instead of:

                raise argparse.ArgumentTypeError("Duplicate OuterIPs found")
    

    I should have:

                raise argparse.ArgumentError(self, "Duplicate OuterIPs found")
    

    And argparse would still do the rest for me (catch exception and print usage message) ...

    0 讨论(0)
提交回复
热议问题