I think you are searching for something like mutual exclusion (at least for the second part of your question).
This way, only foo or bar will be accepted, not both.
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--foo',action=.....)
group.add_argument('--bar',action=.....)
args = parser.parse_args()
BTW, just found another question referring to the same kind of issue.
If you need some check that is not provided by the module you can always do it manually:
pa = argparse.ArgumentParser()
...
args = pa.parse_args()
if args.foo is None and args.bar is None:
pa.error("at least one of --foo and --bar required")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With