Python Argparse:需要一个或另一个的互斥组

Python Argparse: Mutually exclusive group where one or the other are required

我正在尝试构建一个输入列表,其中有两个选项 -

domains.add_argument(
    '-d', dest='domain', required=True,
    help=   'Specify a target domain name'
)

domain - 这将接受域的输入。再次相同,但是 -dL(或 domainlist)。如果解析域,则不需要域列表,但必须提供其中之一。

我已尝试通过在互斥组中按要求添加这两个对象来实现此目的,但是出现的错误指出互斥组中的任何对象都不是必需的。

我可以在启动时对这个检查进行硬编码(既不需要也不需要自己处理),但我确信 argparse 可以做到,有人可以轻推我一下吗,因为到目前为止我还没有在文档中找到它。

add_mutually_exclusive_group() function according to docs 具有完全符合您要求的选项:

Create a mutually exclusive group. argparse will make sure that only one of the arguments in the mutually exclusive group was present on the command line: