简体   繁体   中英

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

I'm trying to build an input list where I have two options -

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

domain - which would take input of a domain. And the same again, but -dL (or domainlist ). If a domain is parsed then domainslist isn't required, but one of them must be provided.

I've tried to do this by adding both of these as required in a mutually exclusive group, however the error I'm presented with states that no objects in a mutually exclusive group can be required.

I could hardcode this check on startup (make neither required and handle myself) but I'm sure argparse can do it, can somebody gfive me a nudge as I haven't found this in the documentation thus far.

The add_mutually_exclusive_group() function according to docs has required option that does exactly what you want:

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM