Michael A
Michael A

Reputation: 9910

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.

Upvotes: 2

Views: 2007

Answers (1)

leotrubach
leotrubach

Reputation: 1597

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:

Upvotes: 2

Related Questions