Skip to content
Advertisement

Create Mutual Inclusive arguments with argparse

I want to build a program that has 2 mutually inclusive args and 2 that are not dependent on the first two. Something like this:

JavaScript

where ‘consume’ and ‘–count’ are dependent on each other i.e, without ‘consume’ ‘–count’ will throw an error ‘show’ and ‘clear’ do not depend on the first consume and –count.

Edit: show and clear are optional arguments

Here’s what I was able to do till now:

JavaScript

So when the run the command, without consume:

JavaScript

I get the following error:

JavaScript

Can someone help me with making the arguments mutually inclusive and other arguments not dependent on them?

Advertisement

Answer

You could try something like this…

Add a MutuallyInclusiveArgumentError Exception subclass, then split the arguments in an argument group, and check for errors after parsing has been completed.

JavaScript

the help message looks like this

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement