Given:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo')
parser.add_argument('--bar')
print(parser.parse_args('--foo 1'.split()))
How do I
--foo x
, --bar y
and --foo x --bar y
are fine--foo x
or --bar y
are fine, --foo x --bar y
is notI 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.